[clang] 352a2e6 - [clang][tooling] NFC: Refactor command-line diagnostic tests
Jan Svoboda via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 23 00:32:21 PDT 2021
Author: Jan Svoboda
Date: 2021-09-23T09:08:53+02:00
New Revision: 352a2e67162541efe79a1e144b40a054751e7d5c
URL: https://github.com/llvm/llvm-project/commit/352a2e67162541efe79a1e144b40a054751e7d5c
DIFF: https://github.com/llvm/llvm-project/commit/352a2e67162541efe79a1e144b40a054751e7d5c.diff
LOG: [clang][tooling] NFC: Refactor command-line diagnostic tests
This patch uses a different command-line arguments to test `clang::tooling::ToolInvocation` that are not specific to Darwin.
Reviewed By: dexonsmith
Differential Revision: https://reviews.llvm.org/D110160
Added:
Modified:
clang/unittests/Tooling/ToolingTest.cpp
Removed:
################################################################################
diff --git a/clang/unittests/Tooling/ToolingTest.cpp b/clang/unittests/Tooling/ToolingTest.cpp
index d5aa8b763669a..40a0fad9a23a1 100644
--- a/clang/unittests/Tooling/ToolingTest.cpp
+++ b/clang/unittests/Tooling/ToolingTest.cpp
@@ -15,6 +15,7 @@
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/FrontendActions.h"
+#include "clang/Frontend/TextDiagnosticBuffer.h"
#include "clang/Tooling/ArgumentsAdjusters.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "clang/Tooling/Tooling.h"
@@ -224,16 +225,6 @@ TEST(ToolInvocation, TestVirtualModulesCompilation) {
EXPECT_TRUE(Invocation.run());
}
-struct ErrorCountingDiagnosticConsumer : public DiagnosticConsumer {
- ErrorCountingDiagnosticConsumer() : NumErrorsSeen(0) {}
- void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
- const Diagnostic &Info) override {
- if (DiagLevel == DiagnosticsEngine::Level::Error)
- ++NumErrorsSeen;
- }
- unsigned NumErrorsSeen;
-};
-
TEST(ToolInvocation, DiagnosticsEngineProperlyInitializedForCC1Construction) {
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
@@ -245,24 +236,22 @@ TEST(ToolInvocation, DiagnosticsEngineProperlyInitializedForCC1Construction) {
std::vector<std::string> Args;
Args.push_back("tool-executable");
- Args.push_back("-target");
- // Invalid argument that by default results in an error diagnostic:
- Args.push_back("i386-apple-ios14.0-simulator");
- // Argument that downgrades the error into a warning:
- Args.push_back("-Wno-error=invalid-ios-deployment-target");
- Args.push_back("-fsyntax-only");
+ // Unknown warning option will result in a warning.
+ Args.push_back("-fexpensive-optimizations");
+ // Argument that will suppress the warning above.
+ Args.push_back("-Wno-ignored-optimization-argument");
+ Args.push_back("-E");
Args.push_back("test.cpp");
clang::tooling::ToolInvocation Invocation(
Args, std::make_unique<SyntaxOnlyAction>(), Files.get());
- InMemoryFileSystem->addFile(
- "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int a() {}\n"));
- ErrorCountingDiagnosticConsumer Consumer;
+ InMemoryFileSystem->addFile("test.cpp", 0,
+ llvm::MemoryBuffer::getMemBuffer(""));
+ TextDiagnosticBuffer Consumer;
Invocation.setDiagnosticConsumer(&Consumer);
EXPECT_TRUE(Invocation.run());
- // Check that `-Wno-error=invalid-ios-deployment-target` downgraded the error
- // into a warning.
- EXPECT_EQ(Consumer.NumErrorsSeen, 0u);
+ // Check that the warning was ignored due to the '-Wno-xxx' argument.
+ EXPECT_EQ(std::distance(Consumer.warn_begin(), Consumer.warn_end()), 0u);
}
TEST(ToolInvocation, CustomDiagnosticOptionsOverwriteParsedOnes) {
@@ -276,28 +265,28 @@ TEST(ToolInvocation, CustomDiagnosticOptionsOverwriteParsedOnes) {
std::vector<std::string> Args;
Args.push_back("tool-executable");
- Args.push_back("-target");
- // Invalid argument that by default results in an error diagnostic:
- Args.push_back("i386-apple-ios14.0-simulator");
- // Argument that downgrades the error into a warning:
- Args.push_back("-Wno-error=invalid-ios-deployment-target");
- Args.push_back("-fsyntax-only");
+ // Unknown warning option will result in a warning.
+ Args.push_back("-fexpensive-optimizations");
+ // Argument that will suppress the warning above.
+ Args.push_back("-Wno-ignored-optimization-argument");
+ Args.push_back("-E");
Args.push_back("test.cpp");
clang::tooling::ToolInvocation Invocation(
Args, std::make_unique<SyntaxOnlyAction>(), Files.get());
- InMemoryFileSystem->addFile(
- "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int a() {}\n"));
- ErrorCountingDiagnosticConsumer Consumer;
+ InMemoryFileSystem->addFile("test.cpp", 0,
+ llvm::MemoryBuffer::getMemBuffer(""));
+ TextDiagnosticBuffer Consumer;
Invocation.setDiagnosticConsumer(&Consumer);
+ // Inject custom `DiagnosticOptions` for command-line parsing.
auto DiagOpts = llvm::makeIntrusiveRefCnt<DiagnosticOptions>();
Invocation.setDiagnosticOptions(&*DiagOpts);
EXPECT_TRUE(Invocation.run());
- // Check that `-Wno-error=invalid-ios-deployment-target` didn't downgrade the
- // error into a warning due to being overwritten by custom diagnostic options.
- EXPECT_EQ(Consumer.NumErrorsSeen, 1u);
+ // Check that the warning was issued during command-line parsing due to the
+ // custom `DiagnosticOptions` without '-Wno-xxx'.
+ EXPECT_EQ(std::distance(Consumer.warn_begin(), Consumer.warn_end()), 1u);
}
struct DiagnosticConsumerExpectingSourceManager : public DiagnosticConsumer {
More information about the cfe-commits
mailing list