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.
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
virtual bool shouldSuggestEndifComment(StringRef Filename)
Returns true if the check should suggest inserting a trailing comment on the #endif of the header gua...
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:26
HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
Definition: HeaderGuard.h:28
Base class for all clang-tidy checks.
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 ...
static constexpr llvm::StringLiteral Name
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
virtual bool shouldSuggestToAddHeaderGuard(StringRef Filename)
Returns true if the check should add a header guard to the file if it has none.