Line data Source code
1 : //===--- HeaderGuard.h - clang-tidy -----------------------------*- C++ -*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 :
10 : #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
11 : #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
12 :
13 : #include "../ClangTidy.h"
14 : #include "../utils/HeaderFileExtensionsUtils.h"
15 :
16 : namespace clang {
17 : namespace tidy {
18 : namespace utils {
19 :
20 : /// Finds and fixes header guards.
21 : /// The check supports these options:
22 : /// - `HeaderFileExtensions`: a comma-separated list of filename extensions of
23 : /// header files (The filename extension should not contain "." prefix).
24 : /// ",h,hh,hpp,hxx" by default.
25 : /// For extension-less header files, using an empty string or leaving an
26 : /// empty string between "," if there are other filename extensions.
27 14 : class HeaderGuardCheck : public ClangTidyCheck {
28 : public:
29 : HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
30 : : ClangTidyCheck(Name, Context),
31 : RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
32 : "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
33 : utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
34 : HeaderFileExtensions, ',');
35 : }
36 : void registerPPCallbacks(CompilerInstance &Compiler) 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
|