[llvm-branch-commits] [clang-tools-extra] 34d2688 - [clang-tidy] Use a MemoryBufferRef when parsing configuration files.
Nathan James via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Dec 10 06:57:01 PST 2020
Author: Nathan James
Date: 2020-12-10T14:52:45Z
New Revision: 34d2688a50f23b4b15bdeab054e28e033ece9363
URL: https://github.com/llvm/llvm-project/commit/34d2688a50f23b4b15bdeab054e28e033ece9363
DIFF: https://github.com/llvm/llvm-project/commit/34d2688a50f23b4b15bdeab054e28e033ece9363.diff
LOG: [clang-tidy] Use a MemoryBufferRef when parsing configuration files.
Using a MemoryBufferRef, If there is an error parsing, we can point the user to the location of the file.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D93024
Added:
Modified:
clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
clang-tools-extra/clang-tidy/ClangTidyOptions.h
clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
clang-tools-extra/clangd/TidyProvider.cpp
clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp b/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
index 1de1b1baccb5..f17ef716d60f 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
@@ -360,7 +360,7 @@ FileOptionsBaseProvider::tryReadConfigFile(StringRef Directory) {
if ((*Text)->getBuffer().empty())
continue;
llvm::ErrorOr<ClangTidyOptions> ParsedOptions =
- ConfigHandler.second((*Text)->getBuffer());
+ ConfigHandler.second({(*Text)->getBuffer(), ConfigFile});
if (!ParsedOptions) {
if (ParsedOptions.getError())
llvm::errs() << "Error parsing " << ConfigFile << ": "
@@ -380,7 +380,8 @@ std::error_code parseLineFilter(StringRef LineFilter,
return Input.error();
}
-llvm::ErrorOr<ClangTidyOptions> parseConfiguration(StringRef Config) {
+llvm::ErrorOr<ClangTidyOptions>
+parseConfiguration(llvm::MemoryBufferRef Config) {
llvm::yaml::Input Input(Config);
ClangTidyOptions Options;
Input >> Options;
diff --git a/clang-tools-extra/clang-tidy/ClangTidyOptions.h b/clang-tools-extra/clang-tidy/ClangTidyOptions.h
index b4a00f68d6cf..e7cd89951ff9 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyOptions.h
+++ b/clang-tools-extra/clang-tidy/ClangTidyOptions.h
@@ -192,7 +192,7 @@ class FileOptionsBaseProvider : public DefaultOptionsProvider {
// 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.
@@ -308,7 +308,8 @@ 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);
/// Serializes configuration to a YAML-encoded string.
std::string configurationAsText(const ClangTidyOptions &Options);
diff --git a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
index beef79e17769..2748fd9f74a5 100644
--- a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
+++ b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
@@ -312,10 +312,11 @@ static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider(
if (UseColor.getNumOccurrences() > 0)
OverrideOptions.UseColor = UseColor;
- auto LoadConfig = [&](StringRef Configuration)
- -> std::unique_ptr<ClangTidyOptionsProvider> {
+ auto LoadConfig =
+ [&](StringRef Configuration,
+ StringRef Source) -> std::unique_ptr<ClangTidyOptionsProvider> {
llvm::ErrorOr<ClangTidyOptions> ParsedConfig =
- parseConfiguration(Configuration);
+ parseConfiguration(MemoryBufferRef(Configuration, Source));
if (ParsedConfig)
return std::make_unique<ConfigOptionsProvider>(
std::move(GlobalOptions),
@@ -334,18 +335,18 @@ static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider(
}
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
- llvm::MemoryBuffer::getFile(ConfigFile.c_str());
+ llvm::MemoryBuffer::getFile(ConfigFile);
if (std::error_code EC = Text.getError()) {
llvm::errs() << "Error: can't read config-file '" << ConfigFile
<< "': " << EC.message() << "\n";
return nullptr;
}
- return LoadConfig((*Text)->getBuffer());
+ return LoadConfig((*Text)->getBuffer(), ConfigFile);
}
if (Config.getNumOccurrences() > 0)
- return LoadConfig(Config);
+ return LoadConfig(Config, "<command-line-config>");
return std::make_unique<FileOptionsProvider>(
std::move(GlobalOptions), std::move(DefaultOptions),
diff --git a/clang-tools-extra/clangd/TidyProvider.cpp b/clang-tools-extra/clangd/TidyProvider.cpp
index f51ac91aaf36..730a402b5df1 100644
--- a/clang-tools-extra/clangd/TidyProvider.cpp
+++ b/clang-tools-extra/clangd/TidyProvider.cpp
@@ -41,7 +41,8 @@ class DotClangTidyCache : private FileCache {
[this](llvm::Optional<llvm::StringRef> Data) {
Value.reset();
if (Data && !Data->empty()) {
- if (auto Parsed = tidy::parseConfiguration(*Data))
+ if (auto Parsed = tidy::parseConfiguration(
+ llvm::MemoryBufferRef(*Data, path())))
Value = std::make_shared<const tidy::ClangTidyOptions>(
std::move(*Parsed));
else
diff --git a/clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp b/clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
index 7e71f28d18bd..40cd9a5eff15 100644
--- a/clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
+++ b/clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
@@ -72,10 +72,11 @@ TEST(ParseLineFilter, ValidFilter) {
TEST(ParseConfiguration, ValidConfiguration) {
llvm::ErrorOr<ClangTidyOptions> Options =
- parseConfiguration("Checks: \"-*,misc-*\"\n"
- "HeaderFilterRegex: \".*\"\n"
- "AnalyzeTemporaryDtors: true\n"
- "User: some.user");
+ parseConfiguration(llvm::MemoryBufferRef("Checks: \"-*,misc-*\"\n"
+ "HeaderFilterRegex: \".*\"\n"
+ "AnalyzeTemporaryDtors: true\n"
+ "User: some.user",
+ "Options"));
EXPECT_TRUE(!!Options);
EXPECT_EQ("-*,misc-*", *Options->Checks);
EXPECT_EQ(".*", *Options->HeaderFilterRegex);
@@ -83,7 +84,8 @@ TEST(ParseConfiguration, ValidConfiguration) {
}
TEST(ParseConfiguration, MergeConfigurations) {
- llvm::ErrorOr<ClangTidyOptions> Options1 = parseConfiguration(R"(
+ llvm::ErrorOr<ClangTidyOptions> Options1 =
+ parseConfiguration(llvm::MemoryBufferRef(R"(
Checks: "check1,check2"
HeaderFilterRegex: "filter1"
AnalyzeTemporaryDtors: true
@@ -91,9 +93,11 @@ TEST(ParseConfiguration, MergeConfigurations) {
ExtraArgs: ['arg1', 'arg2']
ExtraArgsBefore: ['arg-before1', 'arg-before2']
UseColor: false
- )");
+ )",
+ "Options1"));
ASSERT_TRUE(!!Options1);
- llvm::ErrorOr<ClangTidyOptions> Options2 = parseConfiguration(R"(
+ llvm::ErrorOr<ClangTidyOptions> Options2 =
+ parseConfiguration(llvm::MemoryBufferRef(R"(
Checks: "check3,check4"
HeaderFilterRegex: "filter2"
AnalyzeTemporaryDtors: false
@@ -101,7 +105,8 @@ TEST(ParseConfiguration, MergeConfigurations) {
ExtraArgs: ['arg3', 'arg4']
ExtraArgsBefore: ['arg-before3', 'arg-before4']
UseColor: true
- )");
+ )",
+ "Options2"));
ASSERT_TRUE(!!Options2);
ClangTidyOptions Options = Options1->merge(*Options2, 0);
EXPECT_EQ("check1,check2,check3,check4", *Options.Checks);
More information about the llvm-branch-commits
mailing list