clang-tools 10.0.0

HeaderGuard.h
Go to the documentation of this file.
1 //===--- HeaderGuard.h - clang-tidy -----------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
11
12 #include "../ClangTidy.h"
13 #include "../utils/HeaderFileExtensionsUtils.h"
14
15 namespace clang {
16 namespace tidy {
17 namespace utils {
18
19 /// Finds and fixes header guards.
20 /// The check supports these options:
21 /// - `HeaderFileExtensions`: a comma-separated list of filename extensions of
22 /// header files (The filename extension should not contain "." prefix).
23 /// ",h,hh,hpp,hxx" by default.
24 /// For extension-less header files, using an empty string or leaving an
25 /// empty string between "," if there are other filename extensions.
26 class HeaderGuardCheck : public ClangTidyCheck {
27 public:
28 HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
29 : ClangTidyCheck(Name, Context),
30 RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
31 "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
32 utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
33 HeaderFileExtensions, ',');
34 }
35 void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
36 Preprocessor *ModuleExpanderPP) 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:275
clang::tidy::utils::parseHeaderFileExtensions
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
Definition: HeaderFileExtensionsUtils.cpp:41
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:26
clang::tidy::utils::HeaderGuardCheck::HeaderGuardCheck
HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
Definition: HeaderGuard.h:28
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::utils::defaultHeaderFileExtensions
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
Definition: HeaderFileExtensionsUtils.h:40
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:184
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:285
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:39
clang::tidy::utils::HeaderFileExtensionsSet
llvm::SmallSet< llvm::StringRef, 5 > HeaderFileExtensionsSet
Definition: HeaderFileExtensionsUtils.h:21
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:279
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:28
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
clang::tidy::utils::HeaderGuardCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: HeaderGuard.cpp:269
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:281