clang-tools  15.0.0git
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 "../ClangTidyCheck.h"
13 #include "../utils/FileExtensionsUtils.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 semicolon-separated list of filename
22 /// extensions of header files (The filename extension should not contain
23 /// "." prefix). ";h;hh;hpp;hxx" by default.
24 ///
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::parseFileExtensions(RawStringHeaderFileExtensions,
34  HeaderFileExtensions,
36  }
37  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
38  void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
39  Preprocessor *ModuleExpanderPP) override;
40 
41  /// Ensure that the provided header guard is a non-reserved identifier.
42  std::string sanitizeHeaderGuard(StringRef Guard);
43 
44  /// Returns ``true`` if the check should suggest inserting a trailing comment
45  /// on the ``#endif`` of the header guard. It will use the same name as
46  /// returned by ``HeaderGuardCheck::getHeaderGuard``.
47  virtual bool shouldSuggestEndifComment(StringRef Filename);
48  /// Returns ``true`` if the check should suggest changing an existing header
49  /// guard to the string returned by ``HeaderGuardCheck::getHeaderGuard``.
50  virtual bool shouldFixHeaderGuard(StringRef Filename);
51  /// Returns ``true`` if the check should add a header guard to the file
52  /// if it has none.
53  virtual bool shouldSuggestToAddHeaderGuard(StringRef Filename);
54  /// Returns a replacement for the ``#endif`` line with a comment mentioning
55  /// \p HeaderGuard. The replacement should start with ``endif``.
56  virtual std::string formatEndIf(StringRef HeaderGuard);
57  /// Gets the canonical header guard for a file.
58  virtual std::string getHeaderGuard(StringRef Filename,
59  StringRef OldGuard = StringRef()) = 0;
60 
61 private:
62  std::string RawStringHeaderFileExtensions;
63  utils::FileExtensionsSet HeaderFileExtensions;
64 };
65 
66 } // namespace utils
67 } // namespace tidy
68 } // namespace clang
69 
70 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::utils::HeaderGuardCheck::sanitizeHeaderGuard
std::string sanitizeHeaderGuard(StringRef Guard)
Ensure that the provided header guard is a non-reserved identifier.
Definition: HeaderGuard.cpp:283
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:288
clang::tidy::utils::HeaderGuardCheck::HeaderGuardCheck
HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
Definition: HeaderGuard.h:29
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:39
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:53
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:298
clang::tidy::utils::parseFileExtensions
bool parseFileExtensions(StringRef AllFileExtensions, FileExtensionsSet &FileExtensions, StringRef Delimiters)
Parses header file extensions from a semicolon-separated list.
Definition: FileExtensionsUtils.cpp:35
clang::tidy::utils::defaultFileExtensionDelimiters
StringRef defaultFileExtensionDelimiters()
Returns recommended default value for the list of file extension delimiters.
Definition: FileExtensionsUtils.h:48
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:415
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:294
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:67
clang::tidy::utils::HeaderGuardCheck::getHeaderGuard
virtual std::string getHeaderGuard(StringRef Filename, StringRef OldGuard=StringRef())=0
Gets the canonical header guard for a file.
Name
Token Name
Definition: MacroToEnumCheck.cpp:89
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:277
clang::tidy::utils::HeaderGuardCheck
Finds and fixes header guards.
Definition: HeaderGuard.h:27
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
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:292
clang::tidy::utils::HeaderGuardCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: HeaderGuard.cpp:273
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::utils::defaultHeaderFileExtensions
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
Definition: FileExtensionsUtils.h:38
clang::tidy::utils::FileExtensionsSet
llvm::SmallSet< llvm::StringRef, 5 > FileExtensionsSet
Definition: FileExtensionsUtils.h:22