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.
27 class HeaderGuardCheck : public ClangTidyCheck {
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
clang::tidy::utils::HeaderGuardCheck::shouldSuggestEndifComment
virtual bool shouldSuggestEndifComment(StringRef Filename)
Returns true if the check should suggest inserting a trailing comment on the #endif of the header gua...
Definition: HeaderGuard.cpp:276
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
clang::tidy::utils::parseHeaderFileExtensions
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
Definition: HeaderFileExtensionsUtils.cpp:42
clang::tidy::utils::HeaderGuardCheck::getHeaderGuard
virtual std::string getHeaderGuard(StringRef Filename, StringRef OldGuard=StringRef())=0
Gets the canonical header guard for a file.
clang::tidy::utils::HeaderGuardCheck
Finds and fixes header guards.
Definition: HeaderGuard.h:27
clang::tidy::utils::HeaderGuardCheck::HeaderGuardCheck
HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
Definition: HeaderGuard.h:29
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
clang::tidy::utils::defaultHeaderFileExtensions
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
Definition: HeaderFileExtensionsUtils.h:41
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidy.h:183
clang::tidy::utils::HeaderGuardCheck::formatEndIf
virtual std::string formatEndIf(StringRef HeaderGuard)
Returns a replacement for the #endif line with a comment mentioning HeaderGuard.
Definition: HeaderGuard.cpp:286
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:38
clang::tidy::utils::HeaderFileExtensionsSet
llvm::SmallSet< llvm::StringRef, 5 > HeaderFileExtensionsSet
Definition: HeaderFileExtensionsUtils.h:22
clang::tidy::utils::HeaderGuardCheck::shouldFixHeaderGuard
virtual bool shouldFixHeaderGuard(StringRef Filename)
Returns true if the check should suggest changing an existing header guard to the string returned by ...
Definition: HeaderGuard.cpp:280
clang
Definition: AndroidTidyModule.cpp:28
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:104
clang::tidy::utils::HeaderGuardCheck::registerPPCallbacks
void registerPPCallbacks(CompilerInstance &Compiler) override
Override this to register PPCallbacks with Compiler.
Definition: HeaderGuard.cpp:270
clang::tidy::utils::HeaderGuardCheck::shouldSuggestToAddHeaderGuard
virtual bool shouldSuggestToAddHeaderGuard(StringRef Filename)
Returns true if the check should add a header guard to the file if it has none.
Definition: HeaderGuard.cpp:282