Skip to content

Commit

Permalink
Bump to Clang 12 (#37)
Browse files Browse the repository at this point in the history
Co-authored-by: hristov <Peter.Hristov@cern.ch>
  • Loading branch information
pzhristov and hristov authored Aug 18, 2021
1 parent 05bc055 commit d41ae76
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 69 deletions.
79 changes: 65 additions & 14 deletions ClangTidyCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description,
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);

/// Add a diagnostic with the check's name.
DiagnosticBuilder diag(StringRef Description,
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);

/// Adds a diagnostic to report errors in the check's configuration.
DiagnosticBuilder
configurationDiag(StringRef Description,
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);

/// Should store all options supported by this check with their
/// current values or default values for options that haven't been overridden.
///
Expand All @@ -192,7 +201,8 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
public:
/// Initializes the instance using \p CheckName + "." as a prefix.
OptionsView(StringRef CheckName,
const ClangTidyOptions::OptionMap &CheckOptions);
const ClangTidyOptions::OptionMap &CheckOptions,
ClangTidyContext *Context);

/// Read a named option from the ``Context``.
///
Expand Down Expand Up @@ -268,7 +278,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
if (llvm::Expected<T> ValueOr = get<T>(LocalName))
return *ValueOr;
else
logErrToStdErr(ValueOr.takeError());
reportOptionParsingError(ValueOr.takeError());
return Default;
}

Expand Down Expand Up @@ -314,7 +324,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
if (llvm::Expected<T> ValueOr = getLocalOrGlobal<T>(LocalName))
return *ValueOr;
else
logErrToStdErr(ValueOr.takeError());
reportOptionParsingError(ValueOr.takeError());
return Default;
}

Expand All @@ -330,7 +340,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
/// supply the mapping required to convert between ``T`` and a string.
template <typename T>
std::enable_if_t<std::is_enum<T>::value, llvm::Expected<T>>
get(StringRef LocalName, bool IgnoreCase = false) {
get(StringRef LocalName, bool IgnoreCase = false) const {
if (llvm::Expected<int64_t> ValueOr =
getEnumInt(LocalName, typeEraseMapping<T>(), false, IgnoreCase))
return static_cast<T>(*ValueOr);
Expand All @@ -349,11 +359,11 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
/// supply the mapping required to convert between ``T`` and a string.
template <typename T>
std::enable_if_t<std::is_enum<T>::value, T>
get(StringRef LocalName, T Default, bool IgnoreCase = false) {
get(StringRef LocalName, T Default, bool IgnoreCase = false) const {
if (auto ValueOr = get<T>(LocalName, IgnoreCase))
return *ValueOr;
else
logErrToStdErr(ValueOr.takeError());
reportOptionParsingError(ValueOr.takeError());
return Default;
}

Expand All @@ -370,8 +380,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
/// supply the mapping required to convert between ``T`` and a string.
template <typename T>
std::enable_if_t<std::is_enum<T>::value, llvm::Expected<T>>
getLocalOrGlobal(StringRef LocalName,
bool IgnoreCase = false) {
getLocalOrGlobal(StringRef LocalName, bool IgnoreCase = false) const {
if (llvm::Expected<int64_t> ValueOr =
getEnumInt(LocalName, typeEraseMapping<T>(), true, IgnoreCase))
return static_cast<T>(*ValueOr);
Expand All @@ -391,14 +400,40 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
/// supply the mapping required to convert between ``T`` and a string.
template <typename T>
std::enable_if_t<std::is_enum<T>::value, T>
getLocalOrGlobal(StringRef LocalName, T Default, bool IgnoreCase = false) {
getLocalOrGlobal(StringRef LocalName, T Default,
bool IgnoreCase = false) const {
if (auto ValueOr = getLocalOrGlobal<T>(LocalName, IgnoreCase))
return *ValueOr;
else
logErrToStdErr(ValueOr.takeError());
reportOptionParsingError(ValueOr.takeError());
return Default;
}

/// Returns the value for the option \p LocalName represented as a ``T``.
/// If the option is missing returns None, if the option can't be parsed
/// as a ``T``, log that to stderr and return None.
template <typename T = std::string>
llvm::Optional<T> getOptional(StringRef LocalName) const {
if (auto ValueOr = get<T>(LocalName))
return *ValueOr;
else
reportOptionParsingError(ValueOr.takeError());
return llvm::None;
}

/// Returns the value for the local or global option \p LocalName
/// represented as a ``T``.
/// If the option is missing returns None, if the
/// option can't be parsed as a ``T``, log that to stderr and return None.
template <typename T = std::string>
llvm::Optional<T> getOptionalLocalOrGlobal(StringRef LocalName) const {
if (auto ValueOr = getLocalOrGlobal<T>(LocalName))
return *ValueOr;
else
reportOptionParsingError(ValueOr.takeError());
return llvm::None;
}

/// Stores an option with the check-local name \p LocalName with
/// string value \p Value to \p Options.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
Expand All @@ -420,7 +455,8 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
/// supply the mapping required to convert between ``T`` and a string.
template <typename T>
std::enable_if_t<std::is_enum<T>::value>
store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, T Value) {
store(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
T Value) const {
ArrayRef<std::pair<T, StringRef>> Mapping =
OptionEnumMapping<T>::getEnumMapping();
auto Iter = llvm::find_if(
Expand All @@ -436,11 +472,11 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {

llvm::Expected<int64_t> getEnumInt(StringRef LocalName,
ArrayRef<NameAndValue> Mapping,
bool CheckGlobal, bool IgnoreCase);
bool CheckGlobal, bool IgnoreCase) const;

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

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

std::string NamePrefix;
const ClangTidyOptions::OptionMap &CheckOptions;
ClangTidyContext *Context;
};

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

/// Returns the value for the option \p LocalName.
/// If the option is missing returns None.
template <>
Optional<std::string> ClangTidyCheck::OptionsView::getOptional<std::string>(
StringRef LocalName) const;

/// Returns the value for the local or global option \p LocalName.
/// If the option is missing returns None.
template <>
Optional<std::string>
ClangTidyCheck::OptionsView::getOptionalLocalOrGlobal<std::string>(
StringRef LocalName) const;

} // namespace tidy
} // namespace clang

Expand Down
8 changes: 8 additions & 0 deletions ClangTidyDiagnosticConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ class ClangTidyContext {
StringRef Message,
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);

DiagnosticBuilder diag(StringRef CheckName, StringRef Message,
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);

/// Report any errors to do with reading the configuration using this method.
DiagnosticBuilder
configurationDiag(StringRef Message,
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);

/// Sets the \c SourceManager of the used \c DiagnosticsEngine.
///
/// This is called from the \c ClangTidyCheck base class.
Expand Down
14 changes: 12 additions & 2 deletions ClangTidyForceLinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYFORCELINKER_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYFORCELINKER_H

#include "clang/Config/config.h"
#include "clang-tidy-config.h"
#include "llvm/Support/Compiler.h"

namespace clang {
Expand All @@ -20,6 +20,11 @@ extern volatile int AbseilModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED AbseilModuleAnchorDestination =
AbseilModuleAnchorSource;

// This anchor is used to force the linker to link the AlteraModule.
extern volatile int AlteraModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED AlteraModuleAnchorDestination =
AlteraModuleAnchorSource;

// This anchor is used to force the linker to link the AndroidModule.
extern volatile int AndroidModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED AndroidModuleAnchorDestination =
Expand All @@ -40,6 +45,11 @@ extern volatile int CERTModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED CERTModuleAnchorDestination =
CERTModuleAnchorSource;

// This anchor is used to force the linker to link the ConcurrencyModule.
extern volatile int ConcurrencyModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED ConcurrencyModuleAnchorDestination =
ConcurrencyModuleAnchorSource;

// This anchor is used to force the linker to link the CppCoreGuidelinesModule.
extern volatile int CppCoreGuidelinesModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination =
Expand Down Expand Up @@ -90,7 +100,7 @@ extern volatile int ModernizeModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED ModernizeModuleAnchorDestination =
ModernizeModuleAnchorSource;

#if CLANG_ENABLE_STATIC_ANALYZER && \
#if CLANG_TIDY_ENABLE_STATIC_ANALYZER && \
!defined(CLANG_TIDY_DISABLE_STATIC_ANALYZER_CHECKS)
// This anchor is used to force the linker to link the MPIModule.
extern volatile int MPIModuleAnchorSource;
Expand Down
4 changes: 1 addition & 3 deletions ClangTidyModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include <functional>
#include <map>
#include <memory>
#include <string>

namespace clang {
namespace tidy {
Expand Down Expand Up @@ -69,7 +67,7 @@ class ClangTidyCheckFactories {
std::vector<std::unique_ptr<ClangTidyCheck>>
createChecks(ClangTidyContext *Context);

typedef std::map<std::string, CheckFactory> FactoryMap;
typedef llvm::StringMap<CheckFactory> FactoryMap;
FactoryMap::const_iterator begin() const { return Factories.begin(); }
FactoryMap::const_iterator end() const { return Factories.end(); }
bool empty() const { return Factories.empty(); }
Expand Down
67 changes: 37 additions & 30 deletions ClangTidyOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBufferRef.h"
#include "llvm/Support/VirtualFileSystem.h"
#include <functional>
#include <map>
#include <string>
#include <system_error>
#include <utility>
Expand Down Expand Up @@ -56,11 +56,15 @@ struct ClangTidyOptions {
/// of each registered \c ClangTidyModule.
static ClangTidyOptions getDefaults();

/// Overwrites all fields in here by the fields of \p Other that have a value.
/// \p Order specifies precedence of \p Other option.
ClangTidyOptions &mergeWith(const ClangTidyOptions &Other, unsigned Order);

/// Creates a new \c ClangTidyOptions instance combined from all fields
/// of this instance overridden by the fields of \p Other that have a value.
/// \p Order specifies precedence of \p Other option.
ClangTidyOptions mergeWith(const ClangTidyOptions &Other,
unsigned Order) const;
LLVM_NODISCARD ClangTidyOptions merge(const ClangTidyOptions &Other,
unsigned Order) const;

/// Checks filter.
llvm::Optional<std::string> Checks;
Expand Down Expand Up @@ -108,7 +112,7 @@ struct ClangTidyOptions {
unsigned Priority;
};
typedef std::pair<std::string, std::string> StringPair;
typedef std::map<std::string, ClangTidyValue> OptionMap;
typedef llvm::StringMap<ClangTidyValue> OptionMap;

/// Key-value mapping used to store check-specific options.
OptionMap CheckOptions;
Expand Down Expand Up @@ -170,9 +174,10 @@ class ClangTidyOptionsProvider {
/// returns the same options for all files.
class DefaultOptionsProvider : public ClangTidyOptionsProvider {
public:
DefaultOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
const ClangTidyOptions &Options)
: GlobalOptions(GlobalOptions), DefaultOptions(Options) {}
DefaultOptionsProvider(ClangTidyGlobalOptions GlobalOptions,
ClangTidyOptions Options)
: GlobalOptions(std::move(GlobalOptions)),
DefaultOptions(std::move(Options)) {}
const ClangTidyGlobalOptions &getGlobalOptions() override {
return GlobalOptions;
}
Expand All @@ -184,11 +189,11 @@ class DefaultOptionsProvider : public ClangTidyOptionsProvider {
};

class FileOptionsBaseProvider : public DefaultOptionsProvider {
public:
protected:
// A pair of configuration file base name and a function parsing
// configuration from text in the corresponding format.
typedef std::pair<std::string, std::function<llvm::ErrorOr<ClangTidyOptions>(
llvm::StringRef)>>
llvm::MemoryBufferRef)>>
ConfigFileHandler;

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

FileOptionsBaseProvider(
const ClangTidyGlobalOptions &GlobalOptions,
const ClangTidyOptions &DefaultOptions,
const ClangTidyOptions &OverrideOptions,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = nullptr);
FileOptionsBaseProvider(ClangTidyGlobalOptions GlobalOptions,
ClangTidyOptions DefaultOptions,
ClangTidyOptions OverrideOptions,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS);

FileOptionsBaseProvider(const ClangTidyGlobalOptions &GlobalOptions,
const ClangTidyOptions &DefaultOptions,
const ClangTidyOptions &OverrideOptions,
const ConfigFileHandlers &ConfigHandlers);
FileOptionsBaseProvider(ClangTidyGlobalOptions GlobalOptions,
ClangTidyOptions DefaultOptions,
ClangTidyOptions OverrideOptions,
ConfigFileHandlers ConfigHandlers);

protected:
void addRawFileOptions(llvm::StringRef AbsolutePath,
Expand All @@ -240,10 +244,8 @@ class FileOptionsBaseProvider : public DefaultOptionsProvider {
class ConfigOptionsProvider : public FileOptionsBaseProvider {
public:
ConfigOptionsProvider(
const ClangTidyGlobalOptions &GlobalOptions,
const ClangTidyOptions &DefaultOptions,
const ClangTidyOptions &ConfigOptions,
const ClangTidyOptions &OverrideOptions,
ClangTidyGlobalOptions GlobalOptions, ClangTidyOptions DefaultOptions,
ClangTidyOptions ConfigOptions, ClangTidyOptions OverrideOptions,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = nullptr);
std::vector<OptionsSource> getRawOptions(llvm::StringRef FileName) override;

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

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

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

/// Parses configuration from JSON and returns \c ClangTidyOptions or an
/// error.
llvm::ErrorOr<ClangTidyOptions> parseConfiguration(llvm::StringRef Config);
llvm::ErrorOr<ClangTidyOptions>
parseConfiguration(llvm::MemoryBufferRef Config);

using DiagCallback = llvm::function_ref<void(const llvm::SMDiagnostic &)>;

llvm::ErrorOr<ClangTidyOptions>
parseConfigurationWithDiags(llvm::MemoryBufferRef Config, DiagCallback Handler);

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

0 comments on commit d41ae76

Please sign in to comment.