From b37a5e3a71a6f20f9cae60538f6a25942c5256cc Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Tue, 30 Aug 2016 17:42:29 +0000 Subject: [PATCH] Fix colored diagnostics from tools r271042 changed the way the diagnostic arguments are parsed. It assumes that the diagnostics options were already parsed by the "Driver". For tools using clang::Tooling, the diagnostics argument were not parsed. Differential Revision: https://reviews.llvm.org/D23837 llvm-svn: 280118 --- clang/lib/Tooling/Tooling.cpp | 8 +++++++ clang/unittests/Tooling/ToolingTest.cpp | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp index 9611871..162a491 100644 --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/lib/Tooling/Tooling.cpp @@ -15,6 +15,7 @@ #include "clang/Tooling/Tooling.h" #include "clang/Driver/Compilation.h" #include "clang/Driver/Driver.h" +#include "clang/Driver/Options.h" #include "clang/Driver/Tool.h" #include "clang/Driver/ToolChain.h" #include "clang/Frontend/ASTUnit.h" @@ -26,6 +27,7 @@ #include "clang/Tooling/CompilationDatabase.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Config/llvm-config.h" +#include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" #include "llvm/Support/Debug.h" #include "llvm/Support/FileSystem.h" @@ -241,6 +243,12 @@ bool ToolInvocation::run() { Argv.push_back(Str.c_str()); const char *const BinaryName = Argv[0]; IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions(); + unsigned MissingArgIndex, MissingArgCount; + llvm::opt::InputArgList ParsedArgs = + driver::createDriverOptTable()->ParseArgs( + ArrayRef(Argv).slice(1), MissingArgIndex, + MissingArgCount); + ParseDiagnosticArgs(*DiagOpts, ParsedArgs); TextDiagnosticPrinter DiagnosticPrinter( llvm::errs(), &*DiagOpts); DiagnosticsEngine Diagnostics( diff --git a/clang/unittests/Tooling/ToolingTest.cpp b/clang/unittests/Tooling/ToolingTest.cpp index 82ee602..68b5ed2 100644 --- a/clang/unittests/Tooling/ToolingTest.cpp +++ b/clang/unittests/Tooling/ToolingTest.cpp @@ -332,6 +332,44 @@ TEST(runToolOnCodeWithArgs, TestNoDepFile) { EXPECT_FALSE(llvm::sys::fs::remove(DepFilePath.str())); } +struct CheckColoredDiagnosticsAction : public clang::ASTFrontendAction { + CheckColoredDiagnosticsAction(bool ShouldShowColor) + : ShouldShowColor(ShouldShowColor) {} + std::unique_ptr CreateASTConsumer(CompilerInstance &Compiler, + StringRef) override { + if (Compiler.getDiagnosticOpts().ShowColors != ShouldShowColor) + Compiler.getDiagnostics().Report( + Compiler.getDiagnostics().getCustomDiagID( + DiagnosticsEngine::Fatal, + "getDiagnosticOpts().ShowColors != ShouldShowColor")); + return llvm::make_unique(); + } + +private: + bool ShouldShowColor = true; +}; + +TEST(runToolOnCodeWithArgs, DiagnosticsColor) { + + EXPECT_TRUE(runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(true), "", + {"-fcolor-diagnostics"})); + EXPECT_TRUE(runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(false), + "", {"-fno-color-diagnostics"})); + EXPECT_TRUE( + runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(true), "", + {"-fno-color-diagnostics", "-fcolor-diagnostics"})); + EXPECT_TRUE( + runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(false), "", + {"-fcolor-diagnostics", "-fno-color-diagnostics"})); + EXPECT_TRUE(runToolOnCodeWithArgs( + new CheckColoredDiagnosticsAction(true), "", + {"-fno-color-diagnostics", "-fdiagnostics-color=always"})); + + // Check that this test would fail if ShowColors is not what it should. + EXPECT_FALSE(runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(false), + "", {"-fcolor-diagnostics"})); +} + TEST(ClangToolTest, ArgumentAdjusters) { FixedCompilationDatabase Compilations("/", std::vector()); -- 2.7.4