clang-tools  17.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::tidy::utils {
16 
17 /// Finds and fixes header guards.
18 /// The check supports these options:
19 /// - `HeaderFileExtensions`: a semicolon-separated list of filename
20 /// extensions of header files (The filename extension should not contain
21 /// "." prefix). ";h;hh;hpp;hxx" by default.
22 ///
23 /// For extension-less header files, using an empty string or leaving an
24 /// empty string between ";" if there are other filename extensions.
26 public:
27  HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
28  : ClangTidyCheck(Name, Context),
29  RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
30  "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
31  utils::parseFileExtensions(RawStringHeaderFileExtensions,
32  HeaderFileExtensions,
34  }
35  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
36  void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
37  Preprocessor *ModuleExpanderPP) override;
38 
39  /// Ensure that the provided header guard is a non-reserved identifier.
40  std::string sanitizeHeaderGuard(StringRef Guard);
41 
42  /// Returns ``true`` if the check should suggest inserting a trailing comment
43  /// on the ``#endif`` of the header guard. It will use the same name as
44  /// returned by ``HeaderGuardCheck::getHeaderGuard``.
45  virtual bool shouldSuggestEndifComment(StringRef Filename);
46  /// Returns ``true`` if the check should suggest changing an existing header
47  /// guard to the string returned by ``HeaderGuardCheck::getHeaderGuard``.
48  virtual bool shouldFixHeaderGuard(StringRef Filename);
49  /// Returns ``true`` if the check should add a header guard to the file
50  /// if it has none.
51  virtual bool shouldSuggestToAddHeaderGuard(StringRef Filename);
52  /// Returns a replacement for the ``#endif`` line with a comment mentioning
53  /// \p HeaderGuard. The replacement should start with ``endif``.
54  virtual std::string formatEndIf(StringRef HeaderGuard);
55  /// Gets the canonical header guard for a file.
56  virtual std::string getHeaderGuard(StringRef Filename,
57  StringRef OldGuard = StringRef()) = 0;
58 
59 private:
60  std::string RawStringHeaderFileExtensions;
61  utils::FileExtensionsSet HeaderFileExtensions;
62 };
63 
64 } // namespace clang::tidy::utils
65 
66 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:114
clang::tidy::utils::HeaderGuardCheck::sanitizeHeaderGuard
std::string sanitizeHeaderGuard(StringRef Guard)
Ensure that the provided header guard is a non-reserved identifier.
Definition: HeaderGuard.cpp:281
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:286
clang::tidy::utils::HeaderGuardCheck::HeaderGuardCheck
HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
Definition: HeaderGuard.h:27
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:38
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:296
clang::tidy::utils::parseFileExtensions
bool parseFileExtensions(StringRef AllFileExtensions, FileExtensionsSet &FileExtensions, StringRef Delimiters)
Parses header file extensions from a semicolon-separated list.
Definition: FileExtensionsUtils.cpp:34
clang::tidy::utils::defaultFileExtensionDelimiters
StringRef defaultFileExtensionDelimiters()
Returns recommended default value for the list of file extension delimiters.
Definition: FileExtensionsUtils.h:46
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:292
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:68
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:87
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:275
clang::tidy::utils
Definition: Aliasing.cpp:14
clang::tidy::utils::HeaderGuardCheck
Finds and fixes header guards.
Definition: HeaderGuard.h:25
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:28
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:290
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:271
clang::tidy::utils::defaultHeaderFileExtensions
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
Definition: FileExtensionsUtils.h:36
clang::tidy::utils::FileExtensionsSet
llvm::SmallSet< llvm::StringRef, 5 > FileExtensionsSet
Definition: FileExtensionsUtils.h:20