clang-tools 18.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
15namespace 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.
26public:
28 : ClangTidyCheck(Name, Context) {
29 std::optional<StringRef> HeaderFileExtensionsOption =
30 Options.get("HeaderFileExtensions");
31 RawStringHeaderFileExtensions = HeaderFileExtensionsOption.value_or(
33 if (HeaderFileExtensionsOption) {
35 RawStringHeaderFileExtensions, HeaderFileExtensions,
37 this->configurationDiag("Invalid header file extension: '%0'")
38 << RawStringHeaderFileExtensions;
39 }
40 } else
41 HeaderFileExtensions = Context->getHeaderFileExtensions();
42 }
43 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
44 void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
45 Preprocessor *ModuleExpanderPP) override;
46
47 /// Ensure that the provided header guard is a non-reserved identifier.
48 std::string sanitizeHeaderGuard(StringRef Guard);
49
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
58 /// if it has none.
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;
66
67private:
68 std::string RawStringHeaderFileExtensions;
69 FileExtensionsSet HeaderFileExtensions;
70};
71
72} // namespace clang::tidy::utils
73
74#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
llvm::StringRef Name
std::string Filename
Filename as a string.
std::optional< StringRef > get(StringRef LocalName) const
Read a named option from the Context.
Base class for all clang-tidy checks.
DiagnosticBuilder configurationDiag(StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning) const
Adds a diagnostic to report errors in the check's configuration.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Finds and fixes header guards.
Definition: HeaderGuard.h:25
std::string sanitizeHeaderGuard(StringRef Guard)
Ensure that the provided header guard is a non-reserved identifier.
virtual bool shouldSuggestToAddHeaderGuard(StringRef Filename)
Returns true if the check should add a header guard to the file if it has none.
virtual bool shouldFixHeaderGuard(StringRef Filename)
Returns true if the check should suggest changing an existing header guard to the string returned by ...
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
virtual std::string getHeaderGuard(StringRef Filename, StringRef OldGuard=StringRef())=0
Gets the canonical header guard for a file.
HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
Definition: HeaderGuard.h:27
virtual bool shouldSuggestEndifComment(StringRef Filename)
Returns true if the check should suggest inserting a trailing comment on the #endif of the header gua...
virtual std::string formatEndIf(StringRef HeaderGuard)
Returns a replacement for the #endif line with a comment mentioning HeaderGuard.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
StringRef defaultFileExtensionDelimiters()
Returns recommended default value for the list of file extension delimiters.
bool parseFileExtensions(StringRef AllFileExtensions, FileExtensionsSet &FileExtensions, StringRef Delimiters)
Parses header file extensions from a semicolon-separated list.
llvm::SmallSet< llvm::StringRef, 5 > FileExtensionsSet
llvm::StringMap< ClangTidyValue > OptionMap