clang-tools  6.0.0
HeaderGuard.h
Go to the documentation of this file.
1 //===--- HeaderGuard.h - clang-tidy -----------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
12 
13 #include "../ClangTidy.h"
14 #include "../utils/HeaderFileExtensionsUtils.h"
15 
16 namespace clang {
17 namespace tidy {
18 namespace utils {
19 
20 /// Finds and fixes header guards.
21 /// The check supports these options:
22 /// - `HeaderFileExtensions`: a comma-separated list of filename extensions of
23 /// header files (The filename extension should not contain "." prefix).
24 /// ",h,hh,hpp,hxx" by default.
25 /// For extension-less header files, using an empty string or leaving an
26 /// empty string between "," if there are other filename extensions.
28 public:
29  HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
30  : ClangTidyCheck(Name, Context),
31  RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
32  "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
33  utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
34  HeaderFileExtensions, ',');
35  }
36  void registerPPCallbacks(CompilerInstance &Compiler) override;
37 
38  /// Returns ``true`` if the check should suggest inserting a trailing comment
39  /// on the ``#endif`` of the header guard. It will use the same name as
40  /// returned by ``HeaderGuardCheck::getHeaderGuard``.
41  virtual bool shouldSuggestEndifComment(StringRef Filename);
42  /// Returns ``true`` if the check should suggest changing an existing header
43  /// guard to the string returned by ``HeaderGuardCheck::getHeaderGuard``.
44  virtual bool shouldFixHeaderGuard(StringRef Filename);
45  /// Returns ``true`` if the check should add a header guard to the file
46  /// if it has none.
47  virtual bool shouldSuggestToAddHeaderGuard(StringRef Filename);
48  /// Returns a replacement for the ``#endif`` line with a comment mentioning
49  /// \p HeaderGuard. The replacement should start with ``endif``.
50  virtual std::string formatEndIf(StringRef HeaderGuard);
51  /// Gets the canonical header guard for a file.
52  virtual std::string getHeaderGuard(StringRef Filename,
53  StringRef OldGuard = StringRef()) = 0;
54 
55 private:
56  std::string RawStringHeaderFileExtensions;
57  utils::HeaderFileExtensionsSet HeaderFileExtensions;
58 };
59 
60 } // namespace utils
61 } // namespace tidy
62 } // namespace clang
63 
64 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
virtual bool shouldSuggestEndifComment(StringRef Filename)
Returns true if the check should suggest inserting a trailing comment on the #endif of the header gua...
StringHandle Name
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
virtual std::string getHeaderGuard(StringRef Filename, StringRef OldGuard=StringRef())=0
Gets the canonical header guard for a file.
Finds and fixes header guards.
Definition: HeaderGuard.h:27
HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
Definition: HeaderGuard.h:29
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
virtual std::string formatEndIf(StringRef HeaderGuard)
Returns a replacement for the #endif line with a comment mentioning HeaderGuard.
std::string Filename
Filename as a string.
llvm::SmallSet< llvm::StringRef, 5 > HeaderFileExtensionsSet
virtual bool shouldFixHeaderGuard(StringRef Filename)
Returns true if the check should suggest changing an existing header guard to the string returned by ...
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerPPCallbacks(CompilerInstance &Compiler) override
Override this to register PPCallbacks with Compiler.
virtual bool shouldSuggestToAddHeaderGuard(StringRef Filename)
Returns true if the check should add a header guard to the file if it has none.