1 //===--- HeaderGuard.h - clang-tidy -----------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
12 #include "../ClangTidyCheck.h"
13 #include "../utils/FileExtensionsUtils.h"
15 namespace clang::tidy::utils
{
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.
23 /// For extension-less header files, using an empty string or leaving an
24 /// empty string between ";" if there are other filename extensions.
25 class HeaderGuardCheck
: public ClangTidyCheck
{
27 HeaderGuardCheck(StringRef Name
, ClangTidyContext
*Context
)
28 : ClangTidyCheck(Name
, Context
) {
29 std::optional
<StringRef
> HeaderFileExtensionsOption
=
30 Options
.get("HeaderFileExtensions");
31 RawStringHeaderFileExtensions
= HeaderFileExtensionsOption
.value_or(
32 utils::defaultHeaderFileExtensions());
33 if (HeaderFileExtensionsOption
) {
34 if (!utils::parseFileExtensions(
35 RawStringHeaderFileExtensions
, HeaderFileExtensions
,
36 utils::defaultFileExtensionDelimiters())) {
37 this->configurationDiag("Invalid header file extension: '%0'")
38 << RawStringHeaderFileExtensions
;
41 HeaderFileExtensions
= Context
->getHeaderFileExtensions();
43 void storeOptions(ClangTidyOptions::OptionMap
&Opts
) override
;
44 void registerPPCallbacks(const SourceManager
&SM
, Preprocessor
*PP
,
45 Preprocessor
*ModuleExpanderPP
) override
;
47 /// Ensure that the provided header guard is a non-reserved identifier.
48 std::string
sanitizeHeaderGuard(StringRef Guard
);
50 /// Returns ``true`` if the check should suggest inserting a trailing comment
51 /// on the ``#endif`` of the header guard. It will use the same name as
52 /// returned by ``HeaderGuardCheck::getHeaderGuard``.
53 virtual bool shouldSuggestEndifComment(StringRef Filename
);
54 /// Returns ``true`` if the check should suggest changing an existing header
55 /// guard to the string returned by ``HeaderGuardCheck::getHeaderGuard``.
56 virtual bool shouldFixHeaderGuard(StringRef Filename
);
57 /// Returns ``true`` if the check should add a header guard to the file
59 virtual bool shouldSuggestToAddHeaderGuard(StringRef Filename
);
60 /// Returns a replacement for the ``#endif`` line with a comment mentioning
61 /// \p HeaderGuard. The replacement should start with ``endif``.
62 virtual std::string
formatEndIf(StringRef HeaderGuard
);
63 /// Gets the canonical header guard for a file.
64 virtual std::string
getHeaderGuard(StringRef Filename
,
65 StringRef OldGuard
= StringRef()) = 0;
68 std::string RawStringHeaderFileExtensions
;
69 FileExtensionsSet HeaderFileExtensions
;
72 } // namespace clang::tidy::utils
74 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H