Skip to content

Commit d41ae76

Browse files
pzhristovhristov
andauthored
Bump to Clang 12 (#37)
Co-authored-by: hristov <Peter.Hristov@cern.ch>
1 parent 05bc055 commit d41ae76

11 files changed

+212
-69
lines changed

ClangTidyCheck.h

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
176176
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description,
177177
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
178178

179+
/// Add a diagnostic with the check's name.
180+
DiagnosticBuilder diag(StringRef Description,
181+
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
182+
183+
/// Adds a diagnostic to report errors in the check's configuration.
184+
DiagnosticBuilder
185+
configurationDiag(StringRef Description,
186+
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
187+
179188
/// Should store all options supported by this check with their
180189
/// current values or default values for options that haven't been overridden.
181190
///
@@ -192,7 +201,8 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
192201
public:
193202
/// Initializes the instance using \p CheckName + "." as a prefix.
194203
OptionsView(StringRef CheckName,
195-
const ClangTidyOptions::OptionMap &CheckOptions);
204+
const ClangTidyOptions::OptionMap &CheckOptions,
205+
ClangTidyContext *Context);
196206

197207
/// Read a named option from the ``Context``.
198208
///
@@ -268,7 +278,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
268278
if (llvm::Expected<T> ValueOr = get<T>(LocalName))
269279
return *ValueOr;
270280
else
271-
logErrToStdErr(ValueOr.takeError());
281+
reportOptionParsingError(ValueOr.takeError());
272282
return Default;
273283
}
274284

@@ -314,7 +324,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
314324
if (llvm::Expected<T> ValueOr = getLocalOrGlobal<T>(LocalName))
315325
return *ValueOr;
316326
else
317-
logErrToStdErr(ValueOr.takeError());
327+
reportOptionParsingError(ValueOr.takeError());
318328
return Default;
319329
}
320330

@@ -330,7 +340,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
330340
/// supply the mapping required to convert between ``T`` and a string.
331341
template <typename T>
332342
std::enable_if_t<std::is_enum<T>::value, llvm::Expected<T>>
333-
get(StringRef LocalName, bool IgnoreCase = false) {
343+
get(StringRef LocalName, bool IgnoreCase = false) const {
334344
if (llvm::Expected<int64_t> ValueOr =
335345
getEnumInt(LocalName, typeEraseMapping<T>(), false, IgnoreCase))
336346
return static_cast<T>(*ValueOr);
@@ -349,11 +359,11 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
349359
/// supply the mapping required to convert between ``T`` and a string.
350360
template <typename T>
351361
std::enable_if_t<std::is_enum<T>::value, T>
352-
get(StringRef LocalName, T Default, bool IgnoreCase = false) {
362+
get(StringRef LocalName, T Default, bool IgnoreCase = false) const {
353363
if (auto ValueOr = get<T>(LocalName, IgnoreCase))
354364
return *ValueOr;
355365
else
356-
logErrToStdErr(ValueOr.takeError());
366+
reportOptionParsingError(ValueOr.takeError());
357367
return Default;
358368
}
359369

@@ -370,8 +380,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
370380
/// supply the mapping required to convert between ``T`` and a string.
371381
template <typename T>
372382
std::enable_if_t<std::is_enum<T>::value, llvm::Expected<T>>
373-
getLocalOrGlobal(StringRef LocalName,
374-
bool IgnoreCase = false) {
383+
getLocalOrGlobal(StringRef LocalName, bool IgnoreCase = false) const {
375384
if (llvm::Expected<int64_t> ValueOr =
376385
getEnumInt(LocalName, typeEraseMapping<T>(), true, IgnoreCase))
377386
return static_cast<T>(*ValueOr);
@@ -391,14 +400,40 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
391400
/// supply the mapping required to convert between ``T`` and a string.
392401
template <typename T>
393402
std::enable_if_t<std::is_enum<T>::value, T>
394-
getLocalOrGlobal(StringRef LocalName, T Default, bool IgnoreCase = false) {
403+
getLocalOrGlobal(StringRef LocalName, T Default,
404+
bool IgnoreCase = false) const {
395405
if (auto ValueOr = getLocalOrGlobal<T>(LocalName, IgnoreCase))
396406
return *ValueOr;
397407
else
398-
logErrToStdErr(ValueOr.takeError());
408+
reportOptionParsingError(ValueOr.takeError());
399409
return Default;
400410
}
401411

412+
/// Returns the value for the option \p LocalName represented as a ``T``.
413+
/// If the option is missing returns None, if the option can't be parsed
414+
/// as a ``T``, log that to stderr and return None.
415+
template <typename T = std::string>
416+
llvm::Optional<T> getOptional(StringRef LocalName) const {
417+
if (auto ValueOr = get<T>(LocalName))
418+
return *ValueOr;
419+
else
420+
reportOptionParsingError(ValueOr.takeError());
421+
return llvm::None;
422+
}
423+
424+
/// Returns the value for the local or global option \p LocalName
425+
/// represented as a ``T``.
426+
/// If the option is missing returns None, if the
427+
/// option can't be parsed as a ``T``, log that to stderr and return None.
428+
template <typename T = std::string>
429+
llvm::Optional<T> getOptionalLocalOrGlobal(StringRef LocalName) const {
430+
if (auto ValueOr = getLocalOrGlobal<T>(LocalName))
431+
return *ValueOr;
432+
else
433+
reportOptionParsingError(ValueOr.takeError());
434+
return llvm::None;
435+
}
436+
402437
/// Stores an option with the check-local name \p LocalName with
403438
/// string value \p Value to \p Options.
404439
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
@@ -420,7 +455,8 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
420455
/// supply the mapping required to convert between ``T`` and a string.
421456
template <typename T>
422457
std::enable_if_t<std::is_enum<T>::value>
423-
store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, T Value) {
458+
store(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
459+
T Value) const {
424460
ArrayRef<std::pair<T, StringRef>> Mapping =
425461
OptionEnumMapping<T>::getEnumMapping();
426462
auto Iter = llvm::find_if(
@@ -436,11 +472,11 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
436472

437473
llvm::Expected<int64_t> getEnumInt(StringRef LocalName,
438474
ArrayRef<NameAndValue> Mapping,
439-
bool CheckGlobal, bool IgnoreCase);
475+
bool CheckGlobal, bool IgnoreCase) const;
440476

441477
template <typename T>
442478
std::enable_if_t<std::is_enum<T>::value, std::vector<NameAndValue>>
443-
typeEraseMapping() {
479+
typeEraseMapping() const {
444480
ArrayRef<std::pair<T, StringRef>> Mapping =
445481
OptionEnumMapping<T>::getEnumMapping();
446482
std::vector<NameAndValue> Result;
@@ -455,10 +491,12 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
455491
void storeInt(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
456492
int64_t Value) const;
457493

458-
static void logErrToStdErr(llvm::Error &&Err);
494+
/// Emits a diagnostic if \p Err is not a MissingOptionError.
495+
void reportOptionParsingError(llvm::Error &&Err) const;
459496

460497
std::string NamePrefix;
461498
const ClangTidyOptions::OptionMap &CheckOptions;
499+
ClangTidyContext *Context;
462500
};
463501

464502
private:
@@ -523,6 +561,19 @@ void ClangTidyCheck::OptionsView::store<bool>(
523561
ClangTidyOptions::OptionMap &Options, StringRef LocalName,
524562
bool Value) const;
525563

564+
/// Returns the value for the option \p LocalName.
565+
/// If the option is missing returns None.
566+
template <>
567+
Optional<std::string> ClangTidyCheck::OptionsView::getOptional<std::string>(
568+
StringRef LocalName) const;
569+
570+
/// Returns the value for the local or global option \p LocalName.
571+
/// If the option is missing returns None.
572+
template <>
573+
Optional<std::string>
574+
ClangTidyCheck::OptionsView::getOptionalLocalOrGlobal<std::string>(
575+
StringRef LocalName) const;
576+
526577
} // namespace tidy
527578
} // namespace clang
528579

ClangTidyDiagnosticConsumer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ class ClangTidyContext {
9696
StringRef Message,
9797
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
9898

99+
DiagnosticBuilder diag(StringRef CheckName, StringRef Message,
100+
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
101+
102+
/// Report any errors to do with reading the configuration using this method.
103+
DiagnosticBuilder
104+
configurationDiag(StringRef Message,
105+
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
106+
99107
/// Sets the \c SourceManager of the used \c DiagnosticsEngine.
100108
///
101109
/// This is called from the \c ClangTidyCheck base class.

ClangTidyForceLinker.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYFORCELINKER_H
1010
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYFORCELINKER_H
1111

12-
#include "clang/Config/config.h"
12+
#include "clang-tidy-config.h"
1313
#include "llvm/Support/Compiler.h"
1414

1515
namespace clang {
@@ -20,6 +20,11 @@ extern volatile int AbseilModuleAnchorSource;
2020
static int LLVM_ATTRIBUTE_UNUSED AbseilModuleAnchorDestination =
2121
AbseilModuleAnchorSource;
2222

23+
// This anchor is used to force the linker to link the AlteraModule.
24+
extern volatile int AlteraModuleAnchorSource;
25+
static int LLVM_ATTRIBUTE_UNUSED AlteraModuleAnchorDestination =
26+
AlteraModuleAnchorSource;
27+
2328
// This anchor is used to force the linker to link the AndroidModule.
2429
extern volatile int AndroidModuleAnchorSource;
2530
static int LLVM_ATTRIBUTE_UNUSED AndroidModuleAnchorDestination =
@@ -40,6 +45,11 @@ extern volatile int CERTModuleAnchorSource;
4045
static int LLVM_ATTRIBUTE_UNUSED CERTModuleAnchorDestination =
4146
CERTModuleAnchorSource;
4247

48+
// This anchor is used to force the linker to link the ConcurrencyModule.
49+
extern volatile int ConcurrencyModuleAnchorSource;
50+
static int LLVM_ATTRIBUTE_UNUSED ConcurrencyModuleAnchorDestination =
51+
ConcurrencyModuleAnchorSource;
52+
4353
// This anchor is used to force the linker to link the CppCoreGuidelinesModule.
4454
extern volatile int CppCoreGuidelinesModuleAnchorSource;
4555
static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination =
@@ -90,7 +100,7 @@ extern volatile int ModernizeModuleAnchorSource;
90100
static int LLVM_ATTRIBUTE_UNUSED ModernizeModuleAnchorDestination =
91101
ModernizeModuleAnchorSource;
92102

93-
#if CLANG_ENABLE_STATIC_ANALYZER && \
103+
#if CLANG_TIDY_ENABLE_STATIC_ANALYZER && \
94104
!defined(CLANG_TIDY_DISABLE_STATIC_ANALYZER_CHECKS)
95105
// This anchor is used to force the linker to link the MPIModule.
96106
extern volatile int MPIModuleAnchorSource;

ClangTidyModule.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
#include "llvm/ADT/StringMap.h"
1414
#include "llvm/ADT/StringRef.h"
1515
#include <functional>
16-
#include <map>
1716
#include <memory>
18-
#include <string>
1917

2018
namespace clang {
2119
namespace tidy {
@@ -69,7 +67,7 @@ class ClangTidyCheckFactories {
6967
std::vector<std::unique_ptr<ClangTidyCheck>>
7068
createChecks(ClangTidyContext *Context);
7169

72-
typedef std::map<std::string, CheckFactory> FactoryMap;
70+
typedef llvm::StringMap<CheckFactory> FactoryMap;
7371
FactoryMap::const_iterator begin() const { return Factories.begin(); }
7472
FactoryMap::const_iterator end() const { return Factories.end(); }
7573
bool empty() const { return Factories.empty(); }

ClangTidyOptions.h

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
#include "llvm/ADT/StringMap.h"
1515
#include "llvm/ADT/StringRef.h"
1616
#include "llvm/Support/ErrorOr.h"
17+
#include "llvm/Support/MemoryBufferRef.h"
1718
#include "llvm/Support/VirtualFileSystem.h"
1819
#include <functional>
19-
#include <map>
2020
#include <string>
2121
#include <system_error>
2222
#include <utility>
@@ -56,11 +56,15 @@ struct ClangTidyOptions {
5656
/// of each registered \c ClangTidyModule.
5757
static ClangTidyOptions getDefaults();
5858

59+
/// Overwrites all fields in here by the fields of \p Other that have a value.
60+
/// \p Order specifies precedence of \p Other option.
61+
ClangTidyOptions &mergeWith(const ClangTidyOptions &Other, unsigned Order);
62+
5963
/// Creates a new \c ClangTidyOptions instance combined from all fields
6064
/// of this instance overridden by the fields of \p Other that have a value.
6165
/// \p Order specifies precedence of \p Other option.
62-
ClangTidyOptions mergeWith(const ClangTidyOptions &Other,
63-
unsigned Order) const;
66+
LLVM_NODISCARD ClangTidyOptions merge(const ClangTidyOptions &Other,
67+
unsigned Order) const;
6468

6569
/// Checks filter.
6670
llvm::Optional<std::string> Checks;
@@ -108,7 +112,7 @@ struct ClangTidyOptions {
108112
unsigned Priority;
109113
};
110114
typedef std::pair<std::string, std::string> StringPair;
111-
typedef std::map<std::string, ClangTidyValue> OptionMap;
115+
typedef llvm::StringMap<ClangTidyValue> OptionMap;
112116

113117
/// Key-value mapping used to store check-specific options.
114118
OptionMap CheckOptions;
@@ -170,9 +174,10 @@ class ClangTidyOptionsProvider {
170174
/// returns the same options for all files.
171175
class DefaultOptionsProvider : public ClangTidyOptionsProvider {
172176
public:
173-
DefaultOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
174-
const ClangTidyOptions &Options)
175-
: GlobalOptions(GlobalOptions), DefaultOptions(Options) {}
177+
DefaultOptionsProvider(ClangTidyGlobalOptions GlobalOptions,
178+
ClangTidyOptions Options)
179+
: GlobalOptions(std::move(GlobalOptions)),
180+
DefaultOptions(std::move(Options)) {}
176181
const ClangTidyGlobalOptions &getGlobalOptions() override {
177182
return GlobalOptions;
178183
}
@@ -184,11 +189,11 @@ class DefaultOptionsProvider : public ClangTidyOptionsProvider {
184189
};
185190

186191
class FileOptionsBaseProvider : public DefaultOptionsProvider {
187-
public:
192+
protected:
188193
// A pair of configuration file base name and a function parsing
189194
// configuration from text in the corresponding format.
190195
typedef std::pair<std::string, std::function<llvm::ErrorOr<ClangTidyOptions>(
191-
llvm::StringRef)>>
196+
llvm::MemoryBufferRef)>>
192197
ConfigFileHandler;
193198

194199
/// Configuration file handlers listed in the order of priority.
@@ -210,16 +215,15 @@ class FileOptionsBaseProvider : public DefaultOptionsProvider {
210215
/// take precedence over ".clang-tidy" if both reside in the same directory.
211216
typedef std::vector<ConfigFileHandler> ConfigFileHandlers;
212217

213-
FileOptionsBaseProvider(
214-
const ClangTidyGlobalOptions &GlobalOptions,
215-
const ClangTidyOptions &DefaultOptions,
216-
const ClangTidyOptions &OverrideOptions,
217-
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = nullptr);
218+
FileOptionsBaseProvider(ClangTidyGlobalOptions GlobalOptions,
219+
ClangTidyOptions DefaultOptions,
220+
ClangTidyOptions OverrideOptions,
221+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS);
218222

219-
FileOptionsBaseProvider(const ClangTidyGlobalOptions &GlobalOptions,
220-
const ClangTidyOptions &DefaultOptions,
221-
const ClangTidyOptions &OverrideOptions,
222-
const ConfigFileHandlers &ConfigHandlers);
223+
FileOptionsBaseProvider(ClangTidyGlobalOptions GlobalOptions,
224+
ClangTidyOptions DefaultOptions,
225+
ClangTidyOptions OverrideOptions,
226+
ConfigFileHandlers ConfigHandlers);
223227

224228
protected:
225229
void addRawFileOptions(llvm::StringRef AbsolutePath,
@@ -240,10 +244,8 @@ class FileOptionsBaseProvider : public DefaultOptionsProvider {
240244
class ConfigOptionsProvider : public FileOptionsBaseProvider {
241245
public:
242246
ConfigOptionsProvider(
243-
const ClangTidyGlobalOptions &GlobalOptions,
244-
const ClangTidyOptions &DefaultOptions,
245-
const ClangTidyOptions &ConfigOptions,
246-
const ClangTidyOptions &OverrideOptions,
247+
ClangTidyGlobalOptions GlobalOptions, ClangTidyOptions DefaultOptions,
248+
ClangTidyOptions ConfigOptions, ClangTidyOptions OverrideOptions,
247249
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = nullptr);
248250
std::vector<OptionsSource> getRawOptions(llvm::StringRef FileName) override;
249251

@@ -272,9 +274,8 @@ class FileOptionsProvider : public FileOptionsBaseProvider {
272274
/// If any of the \param OverrideOptions fields are set, they will override
273275
/// whatever options are read from the configuration file.
274276
FileOptionsProvider(
275-
const ClangTidyGlobalOptions &GlobalOptions,
276-
const ClangTidyOptions &DefaultOptions,
277-
const ClangTidyOptions &OverrideOptions,
277+
ClangTidyGlobalOptions GlobalOptions, ClangTidyOptions DefaultOptions,
278+
ClangTidyOptions OverrideOptions,
278279
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = nullptr);
279280

280281
/// Initializes the \c FileOptionsProvider instance with a custom set
@@ -294,10 +295,10 @@ class FileOptionsProvider : public FileOptionsBaseProvider {
294295
/// that can parse configuration from this file type. The configuration files
295296
/// in each directory are searched for in the order of appearance in
296297
/// \p ConfigHandlers.
297-
FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
298-
const ClangTidyOptions &DefaultOptions,
299-
const ClangTidyOptions &OverrideOptions,
300-
const ConfigFileHandlers &ConfigHandlers);
298+
FileOptionsProvider(ClangTidyGlobalOptions GlobalOptions,
299+
ClangTidyOptions DefaultOptions,
300+
ClangTidyOptions OverrideOptions,
301+
ConfigFileHandlers ConfigHandlers);
301302

302303
std::vector<OptionsSource> getRawOptions(llvm::StringRef FileName) override;
303304
};
@@ -308,7 +309,13 @@ std::error_code parseLineFilter(llvm::StringRef LineFilter,
308309

309310
/// Parses configuration from JSON and returns \c ClangTidyOptions or an
310311
/// error.
311-
llvm::ErrorOr<ClangTidyOptions> parseConfiguration(llvm::StringRef Config);
312+
llvm::ErrorOr<ClangTidyOptions>
313+
parseConfiguration(llvm::MemoryBufferRef Config);
314+
315+
using DiagCallback = llvm::function_ref<void(const llvm::SMDiagnostic &)>;
316+
317+
llvm::ErrorOr<ClangTidyOptions>
318+
parseConfigurationWithDiags(llvm::MemoryBufferRef Config, DiagCallback Handler);
312319

313320
/// Serializes configuration to a YAML-encoded string.
314321
std::string configurationAsText(const ClangTidyOptions &Options);

0 commit comments

Comments
 (0)