clang-tools
20.0.0git
llvm-project
clang-tools-extra
clang-tidy
utils
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
15
namespace
clang::tidy::utils
{
16
17
/// Finds and fixes header guards.
18
class
HeaderGuardCheck
:
public
ClangTidyCheck
{
19
public
:
20
HeaderGuardCheck
(StringRef
Name
,
ClangTidyContext
*Context)
21
:
ClangTidyCheck
(
Name
, Context),
22
HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
23
24
void
registerPPCallbacks
(
const
SourceManager &SM, Preprocessor *PP,
25
Preprocessor *ModuleExpanderPP)
override
;
26
27
/// Ensure that the provided header guard is a non-reserved identifier.
28
std::string
sanitizeHeaderGuard
(StringRef Guard);
29
30
/// Returns ``true`` if the check should suggest inserting a trailing comment
31
/// on the ``#endif`` of the header guard. It will use the same name as
32
/// returned by ``HeaderGuardCheck::getHeaderGuard``.
33
virtual
bool
shouldSuggestEndifComment
(StringRef
Filename
);
34
/// Returns ``true`` if the check should suggest changing an existing header
35
/// guard to the string returned by ``HeaderGuardCheck::getHeaderGuard``.
36
virtual
bool
shouldFixHeaderGuard
(StringRef
Filename
);
37
/// Returns ``true`` if the check should add a header guard to the file
38
/// if it has none.
39
virtual
bool
shouldSuggestToAddHeaderGuard
(StringRef
Filename
);
40
/// Returns a replacement for the ``#endif`` line with a comment mentioning
41
/// \p HeaderGuard. The replacement should start with ``endif``.
42
virtual
std::string
formatEndIf
(StringRef HeaderGuard);
43
/// Gets the canonical header guard for a file.
44
virtual
std::string
getHeaderGuard
(StringRef
Filename
,
45
StringRef OldGuard = StringRef()) = 0;
46
47
private
:
48
FileExtensionsSet
HeaderFileExtensions;
49
};
50
51
}
// namespace clang::tidy::utils
52
53
#endif
// LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
Name
llvm::SmallString< 256U > Name
Definition:
ChainedComparisonCheck.cpp:42
Filename
std::string Filename
Filename as a string.
Definition:
IncludeOrderCheck.cpp:39
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition:
ClangTidyCheck.h:53
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition:
ClangTidyDiagnosticConsumer.h:69
clang::tidy::utils::HeaderGuardCheck
Finds and fixes header guards.
Definition:
HeaderGuard.h:18
clang::tidy::utils::HeaderGuardCheck::sanitizeHeaderGuard
std::string sanitizeHeaderGuard(StringRef Guard)
Ensure that the provided header guard is a non-reserved identifier.
Definition:
HeaderGuard.cpp:278
clang::tidy::utils::HeaderGuardCheck::shouldSuggestToAddHeaderGuard
virtual bool shouldSuggestToAddHeaderGuard(StringRef Filename)
Returns true if the check should add a header guard to the file if it has none.
Definition:
HeaderGuard.cpp:289
clang::tidy::utils::HeaderGuardCheck::shouldFixHeaderGuard
virtual bool shouldFixHeaderGuard(StringRef Filename)
Returns true if the check should suggest changing an existing header guard to the string returned by ...
Definition:
HeaderGuard.cpp:287
clang::tidy::utils::HeaderGuardCheck::getHeaderGuard
virtual std::string getHeaderGuard(StringRef Filename, StringRef OldGuard=StringRef())=0
Gets the canonical header guard for a file.
clang::tidy::utils::HeaderGuardCheck::HeaderGuardCheck
HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
Definition:
HeaderGuard.h:20
clang::tidy::utils::HeaderGuardCheck::shouldSuggestEndifComment
virtual bool shouldSuggestEndifComment(StringRef Filename)
Returns true if the check should suggest inserting a trailing comment on the #endif of the header gua...
Definition:
HeaderGuard.cpp:283
clang::tidy::utils::HeaderGuardCheck::formatEndIf
virtual std::string formatEndIf(StringRef HeaderGuard)
Returns a replacement for the #endif line with a comment mentioning HeaderGuard.
Definition:
HeaderGuard.cpp:293
clang::tidy::utils::HeaderGuardCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition:
HeaderGuard.cpp:272
clang::tidy::utils
Definition:
Aliasing.cpp:14
clang::tidy::FileExtensionsSet
llvm::SmallSet< llvm::StringRef, 5 > FileExtensionsSet
Definition:
FileExtensionsSet.h:16
Generated on Sat Dec 21 2024 14:59:10 for clang-tools by
1.9.6