Skip to content

Commit

Permalink
Merge from 'master' to 'sycl-web' (intel#36)
Browse files Browse the repository at this point in the history
  CONFLICT (modify/delete): llvm/test/tools/llvm-profdata/show-prof-size.test deleted in e3ba652 and modified in HEAD. Version HEAD of llvm/test/tools/llvm-profdata/show-prof-size.test left in tree.
  • Loading branch information
iclsrc committed Apr 8, 2020
2 parents ebdffb7 + e3ba652 commit c40f925
Show file tree
Hide file tree
Showing 225 changed files with 3,955 additions and 1,797 deletions.
149 changes: 142 additions & 7 deletions clang-tools-extra/clang-tidy/ClangTidyCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,44 @@
//===----------------------------------------------------------------------===//

#include "ClangTidyCheck.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/raw_ostream.h"

namespace clang {
namespace tidy {

char MissingOptionError::ID;
char UnparseableEnumOptionError::ID;
char UnparseableIntegerOptionError::ID;

std::string MissingOptionError::message() const {
llvm::SmallString<128> Buffer;
llvm::raw_svector_ostream Output(Buffer);
Output << "option not found '" << OptionName << '\'';
return std::string(Buffer);
}

std::string UnparseableEnumOptionError::message() const {
llvm::SmallString<128> Buffer;
llvm::raw_svector_ostream Output(Buffer);
Output << "invalid configuration value '" << LookupValue << "' for option '"
<< LookupName << '\'';
if (SuggestedValue)
Output << "; did you mean '" << *SuggestedValue << "'?";
return std::string(Buffer);
}

std::string UnparseableIntegerOptionError::message() const {
llvm::SmallString<128> Buffer;
llvm::raw_svector_ostream Output(Buffer);
Output << "invalid configuration value '" << LookupValue << "' for option '"
<< LookupName << "'; expected "
<< (IsBoolean ? "a bool" : "an integer value");
return std::string(Buffer);
}

ClangTidyCheck::ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context)
: CheckName(CheckName), Context(Context),
Options(CheckName, Context->getOptions().CheckOptions) {
Expand All @@ -34,25 +68,82 @@ ClangTidyCheck::OptionsView::OptionsView(StringRef CheckName,
const ClangTidyOptions::OptionMap &CheckOptions)
: NamePrefix(CheckName.str() + "."), CheckOptions(CheckOptions) {}

std::string ClangTidyCheck::OptionsView::get(StringRef LocalName,
StringRef Default) const {
llvm::Expected<std::string>
ClangTidyCheck::OptionsView::get(StringRef LocalName) const {
const auto &Iter = CheckOptions.find(NamePrefix + LocalName.str());
if (Iter != CheckOptions.end())
return Iter->second;
return std::string(Default);
return llvm::make_error<MissingOptionError>((NamePrefix + LocalName).str());
}

std::string
ClangTidyCheck::OptionsView::getLocalOrGlobal(StringRef LocalName,
StringRef Default) const {
llvm::Expected<std::string>
ClangTidyCheck::OptionsView::getLocalOrGlobal(StringRef LocalName) const {
auto Iter = CheckOptions.find(NamePrefix + LocalName.str());
if (Iter != CheckOptions.end())
return Iter->second;
// Fallback to global setting, if present.
Iter = CheckOptions.find(LocalName.str());
if (Iter != CheckOptions.end())
return Iter->second;
return std::string(Default);
return llvm::make_error<MissingOptionError>((NamePrefix + LocalName).str());
}

static llvm::Expected<bool> getAsBool(StringRef Value,
const llvm::Twine &LookupName) {
if (Value == "true")
return true;
if (Value == "false")
return false;
bool Result;
if (!Value.getAsInteger(10, Result))
return Result;
return llvm::make_error<UnparseableIntegerOptionError>(LookupName.str(),
Value.str(), true);
}

template <>
llvm::Expected<bool>
ClangTidyCheck::OptionsView::get<bool>(StringRef LocalName) const {
llvm::Expected<std::string> ValueOr = get(LocalName);
if (ValueOr)
return getAsBool(*ValueOr, NamePrefix + LocalName);
return ValueOr.takeError();
}

template <>
bool ClangTidyCheck::OptionsView::get<bool>(StringRef LocalName,
bool Default) const {
llvm::Expected<bool> ValueOr = get<bool>(LocalName);
if (ValueOr)
return *ValueOr;
logErrToStdErr(ValueOr.takeError());
return Default;
}

template <>
llvm::Expected<bool>
ClangTidyCheck::OptionsView::getLocalOrGlobal<bool>(StringRef LocalName) const {
llvm::Expected<std::string> ValueOr = get(LocalName);
bool IsGlobal = false;
if (!ValueOr) {
llvm::consumeError(ValueOr.takeError());
ValueOr = getLocalOrGlobal(LocalName);
IsGlobal = true;
}
if (!ValueOr)
return ValueOr.takeError();
return getAsBool(*ValueOr, IsGlobal ? llvm::Twine(LocalName)
: (NamePrefix + LocalName));
}

template <>
bool ClangTidyCheck::OptionsView::getLocalOrGlobal<bool>(StringRef LocalName,
bool Default) const {
llvm::Expected<bool> ValueOr = getLocalOrGlobal<bool>(LocalName);
if (ValueOr)
return *ValueOr;
logErrToStdErr(ValueOr.takeError());
return Default;
}

void ClangTidyCheck::OptionsView::store(ClangTidyOptions::OptionMap &Options,
Expand All @@ -67,5 +158,49 @@ void ClangTidyCheck::OptionsView::store(ClangTidyOptions::OptionMap &Options,
store(Options, LocalName, llvm::itostr(Value));
}

llvm::Expected<int64_t> ClangTidyCheck::OptionsView::getEnumInt(
StringRef LocalName, ArrayRef<std::pair<StringRef, int64_t>> Mapping,
bool CheckGlobal, bool IgnoreCase) {
auto Iter = CheckOptions.find((NamePrefix + LocalName).str());
if (CheckGlobal && Iter == CheckOptions.end())
Iter = CheckOptions.find(LocalName.str());
if (Iter == CheckOptions.end())
return llvm::make_error<MissingOptionError>((NamePrefix + LocalName).str());

StringRef Value = Iter->second;
StringRef Closest;
unsigned EditDistance = -1;
for (const auto &NameAndEnum : Mapping) {
if (IgnoreCase) {
if (Value.equals_lower(NameAndEnum.first))
return NameAndEnum.second;
} else if (Value.equals(NameAndEnum.first)) {
return NameAndEnum.second;
} else if (Value.equals_lower(NameAndEnum.first)) {
Closest = NameAndEnum.first;
EditDistance = 0;
continue;
}
unsigned Distance = Value.edit_distance(NameAndEnum.first);
if (Distance < EditDistance) {
EditDistance = Distance;
Closest = NameAndEnum.first;
}
}
if (EditDistance < 3)
return llvm::make_error<UnparseableEnumOptionError>(
Iter->first, Iter->second, std::string(Closest));
return llvm::make_error<UnparseableEnumOptionError>(Iter->first,
Iter->second);
}

void ClangTidyCheck::OptionsView::logErrToStdErr(llvm::Error &&Err) {
llvm::logAllUnhandledErrors(
llvm::handleErrors(std::move(Err),
[](const MissingOptionError &) -> llvm::Error {
return llvm::Error::success();
}),
llvm::errs(), "warning: ");
}
} // namespace tidy
} // namespace clang
Loading

0 comments on commit c40f925

Please sign in to comment.