Skip to content

Commit a22935e

Browse files
committed
Bump to Clang 15
1 parent 9b46b24 commit a22935e

File tree

9 files changed

+278
-65
lines changed

9 files changed

+278
-65
lines changed

ClangTidy.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "ClangTidyDiagnosticConsumer.h"
1313
#include "ClangTidyOptions.h"
14+
#include "llvm/ADT/StringSet.h"
1415
#include <memory>
1516
#include <vector>
1617

@@ -38,7 +39,7 @@ class ClangTidyASTConsumerFactory {
3839

3940
/// Returns an ASTConsumer that runs the specified clang-tidy checks.
4041
std::unique_ptr<clang::ASTConsumer>
41-
CreateASTConsumer(clang::CompilerInstance &Compiler, StringRef File);
42+
createASTConsumer(clang::CompilerInstance &Compiler, StringRef File);
4243

4344
/// Get the list of enabled checks.
4445
std::vector<std::string> getCheckNames();
@@ -57,6 +58,14 @@ class ClangTidyASTConsumerFactory {
5758
std::vector<std::string> getCheckNames(const ClangTidyOptions &Options,
5859
bool AllowEnablingAnalyzerAlphaCheckers);
5960

61+
struct NamesAndOptions {
62+
llvm::StringSet<> Names;
63+
llvm::StringSet<> Options;
64+
};
65+
66+
NamesAndOptions
67+
getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers = true);
68+
6069
/// Returns the effective check-specific options.
6170
///
6271
/// The method configures ClangTidy with the specified \p Options and collects

ClangTidyCheck.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
namespace clang {
2222

23-
class CompilerInstance;
2423
class SourceManager;
2524

2625
namespace tidy {
@@ -123,7 +122,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
123122
/// Adds a diagnostic to report errors in the check's configuration.
124123
DiagnosticBuilder
125124
configurationDiag(StringRef Description,
126-
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
125+
DiagnosticIDs::Level Level = DiagnosticIDs::Warning) const;
127126

128127
/// Should store all options supported by this check with their
129128
/// current values or default values for options that haven't been overridden.
@@ -156,30 +155,30 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
156155
/// Reads the option with the check-local name \p LocalName from the
157156
/// ``CheckOptions``. If the corresponding key is not present, return
158157
/// ``None``.
159-
llvm::Optional<std::string> get(StringRef LocalName) const;
158+
llvm::Optional<StringRef> get(StringRef LocalName) const;
160159

161160
/// Read a named option from the ``Context``.
162161
///
163162
/// Reads the option with the check-local name \p LocalName from the
164163
/// ``CheckOptions``. If the corresponding key is not present, returns
165164
/// \p Default.
166-
std::string get(StringRef LocalName, StringRef Default) const;
165+
StringRef get(StringRef LocalName, StringRef Default) const;
167166

168167
/// Read a named option from the ``Context``.
169168
///
170169
/// Reads the option with the check-local name \p LocalName from local or
171170
/// global ``CheckOptions``. Gets local option first. If local is not
172171
/// present, falls back to get global option. If global option is not
173172
/// present either, return ``None``.
174-
llvm::Optional<std::string> getLocalOrGlobal(StringRef LocalName) const;
173+
llvm::Optional<StringRef> getLocalOrGlobal(StringRef LocalName) const;
175174

176175
/// Read a named option from the ``Context``.
177176
///
178177
/// Reads the option with the check-local name \p LocalName from local or
179178
/// global ``CheckOptions``. Gets local option first. If local is not
180179
/// present, falls back to get global option. If global option is not
181180
/// present either, returns \p Default.
182-
std::string getLocalOrGlobal(StringRef LocalName, StringRef Default) const;
181+
StringRef getLocalOrGlobal(StringRef LocalName, StringRef Default) const;
183182

184183
/// Read a named option from the ``Context`` and parse it as an
185184
/// integral type ``T``.
@@ -193,7 +192,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
193192
template <typename T>
194193
std::enable_if_t<std::is_integral<T>::value, llvm::Optional<T>>
195194
get(StringRef LocalName) const {
196-
if (llvm::Optional<std::string> Value = get(LocalName)) {
195+
if (llvm::Optional<StringRef> Value = get(LocalName)) {
197196
T Result{};
198197
if (!StringRef(*Value).getAsInteger(10, Result))
199198
return Result;
@@ -214,7 +213,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
214213
template <typename T>
215214
std::enable_if_t<std::is_integral<T>::value, T> get(StringRef LocalName,
216215
T Default) const {
217-
return get<T>(LocalName).getValueOr(Default);
216+
return get<T>(LocalName).value_or(Default);
218217
}
219218

220219
/// Read a named option from the ``Context`` and parse it as an
@@ -230,7 +229,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
230229
template <typename T>
231230
std::enable_if_t<std::is_integral<T>::value, llvm::Optional<T>>
232231
getLocalOrGlobal(StringRef LocalName) const {
233-
llvm::Optional<std::string> ValueOr = get(LocalName);
232+
llvm::Optional<StringRef> ValueOr = get(LocalName);
234233
bool IsGlobal = false;
235234
if (!ValueOr) {
236235
IsGlobal = true;
@@ -259,7 +258,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
259258
template <typename T>
260259
std::enable_if_t<std::is_integral<T>::value, T>
261260
getLocalOrGlobal(StringRef LocalName, T Default) const {
262-
return getLocalOrGlobal<T>(LocalName).getValueOr(Default);
261+
return getLocalOrGlobal<T>(LocalName).value_or(Default);
263262
}
264263

265264
/// Read a named option from the ``Context`` and parse it as an
@@ -298,7 +297,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
298297
template <typename T>
299298
std::enable_if_t<std::is_enum<T>::value, T>
300299
get(StringRef LocalName, T Default, bool IgnoreCase = false) const {
301-
return get<T>(LocalName, IgnoreCase).getValueOr(Default);
300+
return get<T>(LocalName, IgnoreCase).value_or(Default);
302301
}
303302

304303
/// Read a named option from the ``Context`` and parse it as an
@@ -340,7 +339,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
340339
std::enable_if_t<std::is_enum<T>::value, T>
341340
getLocalOrGlobal(StringRef LocalName, T Default,
342341
bool IgnoreCase = false) const {
343-
return getLocalOrGlobal<T>(LocalName, IgnoreCase).getValueOr(Default);
342+
return getLocalOrGlobal<T>(LocalName, IgnoreCase).value_or(Default);
344343
}
345344

346345
/// Stores an option with the check-local name \p LocalName with
@@ -418,6 +417,11 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
418417
StringRef getCurrentMainFile() const { return Context->getCurrentFile(); }
419418
/// Returns the language options from the context.
420419
const LangOptions &getLangOpts() const { return Context->getLangOpts(); }
420+
/// Returns true when the check is run in a use case when only 1 fix will be
421+
/// applied at a time.
422+
bool areDiagsSelfContained() const {
423+
return Context->areDiagsSelfContained();
424+
}
421425
};
422426

423427
/// Read a named option from the ``Context`` and parse it as a bool.

ClangTidyDiagnosticConsumer.h

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,20 @@
1111

1212
#include "ClangTidyOptions.h"
1313
#include "ClangTidyProfiling.h"
14+
#include "NoLintDirectiveHandler.h"
1415
#include "clang/Basic/Diagnostic.h"
1516
#include "clang/Tooling/Core/Diagnostic.h"
1617
#include "llvm/ADT/DenseMap.h"
18+
#include "llvm/ADT/StringSet.h"
1719
#include "llvm/Support/Regex.h"
1820

1921
namespace clang {
2022

2123
class ASTContext;
22-
class CompilerInstance;
2324
class SourceManager;
24-
namespace ast_matchers {
25-
class MatchFinder;
26-
}
27-
namespace tooling {
28-
class CompilationDatabase;
29-
}
3025

3126
namespace tidy {
27+
class CachedGlobList;
3228

3329
/// A detected error complete with information to display diagnostic and
3430
/// automatic fix.
@@ -45,18 +41,13 @@ struct ClangTidyError : tooling::Diagnostic {
4541
std::vector<std::string> EnabledDiagnosticAliases;
4642
};
4743

48-
/// Contains displayed and ignored diagnostic counters for a ClangTidy
49-
/// run.
44+
/// Contains displayed and ignored diagnostic counters for a ClangTidy run.
5045
struct ClangTidyStats {
51-
ClangTidyStats()
52-
: ErrorsDisplayed(0), ErrorsIgnoredCheckFilter(0), ErrorsIgnoredNOLINT(0),
53-
ErrorsIgnoredNonUserCode(0), ErrorsIgnoredLineFilter(0) {}
54-
55-
unsigned ErrorsDisplayed;
56-
unsigned ErrorsIgnoredCheckFilter;
57-
unsigned ErrorsIgnoredNOLINT;
58-
unsigned ErrorsIgnoredNonUserCode;
59-
unsigned ErrorsIgnoredLineFilter;
46+
unsigned ErrorsDisplayed = 0;
47+
unsigned ErrorsIgnoredCheckFilter = 0;
48+
unsigned ErrorsIgnoredNOLINT = 0;
49+
unsigned ErrorsIgnoredNonUserCode = 0;
50+
unsigned ErrorsIgnoredLineFilter = 0;
6051

6152
unsigned errorsIgnored() const {
6253
return ErrorsIgnoredNOLINT + ErrorsIgnoredCheckFilter +
@@ -99,11 +90,33 @@ class ClangTidyContext {
9990
DiagnosticBuilder diag(StringRef CheckName, StringRef Message,
10091
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
10192

93+
DiagnosticBuilder diag(const tooling::Diagnostic &Error);
94+
10295
/// Report any errors to do with reading the configuration using this method.
10396
DiagnosticBuilder
10497
configurationDiag(StringRef Message,
10598
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
10699

100+
/// Check whether a given diagnostic should be suppressed due to the presence
101+
/// of a "NOLINT" suppression comment.
102+
/// This is exposed so that other tools that present clang-tidy diagnostics
103+
/// (such as clangd) can respect the same suppression rules as clang-tidy.
104+
/// This does not handle suppression of notes following a suppressed
105+
/// diagnostic; that is left to the caller as it requires maintaining state in
106+
/// between calls to this function.
107+
/// If any NOLINT is malformed, e.g. a BEGIN without a subsequent END, output
108+
/// \param NoLintErrors will return an error about it.
109+
/// If \param AllowIO is false, the function does not attempt to read source
110+
/// files from disk which are not already mapped into memory; such files are
111+
/// treated as not containing a suppression comment.
112+
/// \param EnableNoLintBlocks controls whether to honor NOLINTBEGIN/NOLINTEND
113+
/// blocks; if false, only considers line-level disabling.
114+
bool
115+
shouldSuppressDiagnostic(DiagnosticsEngine::Level DiagLevel,
116+
const Diagnostic &Info,
117+
SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
118+
bool AllowIO = true, bool EnableNoLintBlocks = true);
119+
107120
/// Sets the \c SourceManager of the used \c DiagnosticsEngine.
108121
///
109122
/// This is called from the \c ClangTidyCheck base class.
@@ -165,7 +178,7 @@ class ClangTidyContext {
165178
}
166179

167180
/// Returns build directory of the current translation unit.
168-
const std::string &getCurrentBuildDirectory() {
181+
const std::string &getCurrentBuildDirectory() const {
169182
return CurrentBuildDirectory;
170183
}
171184

@@ -175,6 +188,10 @@ class ClangTidyContext {
175188
return AllowEnablingAnalyzerAlphaCheckers;
176189
}
177190

191+
void setSelfContainedDiags(bool Value) { SelfContainedDiags = Value; }
192+
193+
bool areDiagsSelfContained() const { return SelfContainedDiags; }
194+
178195
using DiagLevelAndFormatString = std::pair<DiagnosticIDs::Level, std::string>;
179196
DiagLevelAndFormatString getDiagLevelAndFormatString(unsigned DiagnosticID,
180197
SourceLocation Loc) {
@@ -185,6 +202,11 @@ class ClangTidyContext {
185202
DiagEngine->getDiagnosticIDs()->getDescription(DiagnosticID)));
186203
}
187204

205+
void setOptionsCollector(llvm::StringSet<> *Collector) {
206+
OptionsCollector = Collector;
207+
}
208+
llvm::StringSet<> *getOptionsCollector() const { return OptionsCollector; }
209+
188210
private:
189211
// Writes to Stats.
190212
friend class ClangTidyDiagnosticConsumer;
@@ -194,7 +216,7 @@ class ClangTidyContext {
194216

195217
std::string CurrentFile;
196218
ClangTidyOptions CurrentOptions;
197-
class CachedGlobList;
219+
198220
std::unique_ptr<CachedGlobList> CheckFilter;
199221
std::unique_ptr<CachedGlobList> WarningAsErrorFilter;
200222

@@ -210,21 +232,12 @@ class ClangTidyContext {
210232
std::string ProfilePrefix;
211233

212234
bool AllowEnablingAnalyzerAlphaCheckers;
213-
};
214235

215-
/// Check whether a given diagnostic should be suppressed due to the presence
216-
/// of a "NOLINT" suppression comment.
217-
/// This is exposed so that other tools that present clang-tidy diagnostics
218-
/// (such as clangd) can respect the same suppression rules as clang-tidy.
219-
/// This does not handle suppression of notes following a suppressed diagnostic;
220-
/// that is left to the caller is it requires maintaining state in between calls
221-
/// to this function.
222-
/// If `AllowIO` is false, the function does not attempt to read source files
223-
/// from disk which are not already mapped into memory; such files are treated
224-
/// as not containing a suppression comment.
225-
bool shouldSuppressDiagnostic(DiagnosticsEngine::Level DiagLevel,
226-
const Diagnostic &Info, ClangTidyContext &Context,
227-
bool AllowIO = true);
236+
bool SelfContainedDiags;
237+
238+
NoLintDirectiveHandler NoLintHandler;
239+
llvm::StringSet<> *OptionsCollector = nullptr;
240+
};
228241

229242
/// Gets the Fix attached to \p Diagnostic.
230243
/// If there isn't a Fix attached to the diagnostic and \p AnyFix is true, Check
@@ -235,15 +248,17 @@ getFixIt(const tooling::Diagnostic &Diagnostic, bool AnyFix);
235248

236249
/// A diagnostic consumer that turns each \c Diagnostic into a
237250
/// \c SourceManager-independent \c ClangTidyError.
238-
//
239251
// FIXME: If we move away from unit-tests, this can be moved to a private
240252
// implementation file.
241253
class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
242254
public:
255+
/// \param EnableNolintBlocks Enables diagnostic-disabling inside blocks of
256+
/// code, delimited by NOLINTBEGIN and NOLINTEND.
243257
ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx,
244258
DiagnosticsEngine *ExternalDiagEngine = nullptr,
245259
bool RemoveIncompatibleErrors = true,
246-
bool GetFixesFromNotes = false);
260+
bool GetFixesFromNotes = false,
261+
bool EnableNolintBlocks = true);
247262

248263
// FIXME: The concept of converting between FixItHints and Replacements is
249264
// more generic and should be pulled out into a more useful Diagnostics
@@ -274,6 +289,7 @@ class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
274289
DiagnosticsEngine *ExternalDiagEngine;
275290
bool RemoveIncompatibleErrors;
276291
bool GetFixesFromNotes;
292+
bool EnableNolintBlocks;
277293
std::vector<ClangTidyError> Errors;
278294
std::unique_ptr<llvm::Regex> HeaderFilter;
279295
bool LastErrorRelatesToUserCode;

ClangTidyModule.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ class ClangTidyCheckFactories {
6767
std::vector<std::unique_ptr<ClangTidyCheck>>
6868
createChecks(ClangTidyContext *Context);
6969

70+
/// Create instances of checks that are enabled for the current Language.
71+
std::vector<std::unique_ptr<ClangTidyCheck>>
72+
createChecksForLanguage(ClangTidyContext *Context);
73+
7074
typedef llvm::StringMap<CheckFactory> FactoryMap;
7175
FactoryMap::const_iterator begin() const { return Factories.begin(); }
7276
FactoryMap::const_iterator end() const { return Factories.end(); }

ClangTidyOptions.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ struct ClangTidyOptions {
108108

109109
std::string Value;
110110
/// Priority stores relative precedence of the value loaded from config
111-
/// files to disambigute local vs global value from different levels.
111+
/// files to disambiguate local vs global value from different levels.
112112
unsigned Priority;
113113
};
114114
typedef std::pair<std::string, std::string> StringPair;
@@ -129,8 +129,8 @@ struct ClangTidyOptions {
129129
/// and using a FileOptionsProvider, it will take a configuration file in the
130130
/// parent directory (if any exists) and apply this config file on top of the
131131
/// parent one. IF true and using a ConfigOptionsProvider, it will apply this
132-
/// config on top of any configuation file it finds in the directory using the
133-
/// same logic as FileOptionsProvider. If false or missing, only this
132+
/// config on top of any configuration file it finds in the directory using
133+
/// the same logic as FileOptionsProvider. If false or missing, only this
134134
/// configuration file will be used.
135135
llvm::Optional<bool> InheritParentConfig;
136136

0 commit comments

Comments
 (0)