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.
27 class HeaderGuardCheck : public ClangTidyCheck {
28 public:
HeaderGuardCheck(StringRef Name,ClangTidyContext * Context)29   HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
30       : ClangTidyCheck(Name, Context),
31         RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
32             "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
33     utils::parseFileExtensions(RawStringHeaderFileExtensions,
34                                HeaderFileExtensions,
35                                utils::defaultFileExtensionDelimiters());
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
71