Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/ClangTidyModule/UsePragmaOnceCheck.h
3148 views
1
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2
file LICENSE.rst or https://cmake.org/licensing for details. */
3
4
/* This code was originally taken from part of the Clang-Tidy LLVM project and
5
* modified for use with CMake under the following original license: */
6
7
//===--- HeaderGuard.h - clang-tidy -----------------------------*- C++
8
//-*-===//
9
//
10
// Part of the LLVM Project, under the Apache License v2.0 with LLVM
11
// Exceptions. See https://llvm.org/LICENSE.txt for license information.
12
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
13
//
14
//===----------------------------------------------------------------------===//
15
16
#pragma once
17
18
#include <clang-tidy/ClangTidyCheck.h>
19
#include <clang-tidy/utils/FileExtensionsUtils.h>
20
21
#if LLVM_VERSION_MAJOR >= 17
22
# include <clang-tidy/FileExtensionsSet.h>
23
#else
24
namespace clang {
25
namespace tidy {
26
using utils::FileExtensionsSet;
27
} // namespace tidy
28
} // namespace clang
29
#endif
30
31
namespace clang {
32
namespace tidy {
33
namespace cmake {
34
35
/// Finds and replaces header guards with pragma once.
36
/// The check supports these options:
37
/// - `HeaderFileExtensions`: a semicolon-separated list of filename
38
/// extensions of header files (The filename extension should not contain
39
/// "." prefix). ";h;hh;hpp;hxx" by default.
40
///
41
/// For extension-less header files, using an empty string or leaving an
42
/// empty string between ";" if there are other filename extensions.
43
class UsePragmaOnceCheck : public ClangTidyCheck
44
{
45
public:
46
UsePragmaOnceCheck(StringRef Name, ClangTidyContext* Context)
47
: ClangTidyCheck(Name, Context)
48
, RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
49
"HeaderFileExtensions", utils::defaultHeaderFileExtensions()))
50
{
51
utils::parseFileExtensions(RawStringHeaderFileExtensions,
52
HeaderFileExtensions,
53
utils::defaultFileExtensionDelimiters());
54
}
55
void storeOptions(ClangTidyOptions::OptionMap& Opts) override;
56
void registerPPCallbacks(SourceManager const& SM, Preprocessor* PP,
57
Preprocessor* ModuleExpanderPP) override;
58
59
/// Returns ``true`` if the check should add pragma once to the file
60
/// if it has none.
61
virtual bool shouldSuggestToAddPragmaOnce(StringRef Filename);
62
63
private:
64
std::string RawStringHeaderFileExtensions;
65
FileExtensionsSet HeaderFileExtensions;
66
};
67
68
} // namespace cmake
69
} // namespace tidy
70
} // namespace clang
71
72