r290892 - [clang-tidy] Add check name to YAML export
Alexander Kornienko via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 3 06:35:48 PST 2017
Author: alexfh
Date: Tue Jan 3 08:35:47 2017
New Revision: 290892
URL: http://llvm.org/viewvc/llvm-project?rev=290892&view=rev
Log:
[clang-tidy] Add check name to YAML export
Add a field indicating the associated check for every replacement to the YAML
report generated with the '-export-fixes' option. Update
clang-apply-replacements to handle the new format.
Patch by Alpha Abdoulaye!
Differential revision: https://reviews.llvm.org/D26137
Added:
cfe/trunk/include/clang/Tooling/Core/Diagnostic.h
cfe/trunk/include/clang/Tooling/DiagnosticsYaml.h
cfe/trunk/lib/Tooling/Core/Diagnostic.cpp
Modified:
cfe/trunk/include/clang/Tooling/Core/Replacement.h
cfe/trunk/include/clang/Tooling/ReplacementsYaml.h
cfe/trunk/lib/Tooling/Core/CMakeLists.txt
cfe/trunk/unittests/Tooling/ReplacementsYamlTest.cpp
Added: cfe/trunk/include/clang/Tooling/Core/Diagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Core/Diagnostic.h?rev=290892&view=auto
==============================================================================
--- cfe/trunk/include/clang/Tooling/Core/Diagnostic.h (added)
+++ cfe/trunk/include/clang/Tooling/Core/Diagnostic.h Tue Jan 3 08:35:47 2017
@@ -0,0 +1,100 @@
+//===--- Diagnostic.h - Framework for clang diagnostics tools --*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// \file
+// Structures supporting diagnostics and refactorings that span multiple
+// translation units. Indicate diagnostics reports and replacements
+// suggestions for the analyzed sources.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLING_CORE_DIAGNOSTIC_H
+#define LLVM_CLANG_TOOLING_CORE_DIAGNOSTIC_H
+
+#include "Replacement.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringRef.h"
+#include <string>
+
+namespace clang {
+namespace tooling {
+
+/// \brief Represents the diagnostic message with the error message associated
+/// and the information on the location of the problem.
+struct DiagnosticMessage {
+ DiagnosticMessage(llvm::StringRef Message = "");
+
+ /// \brief Constructs a diagnostic message with anoffset to the diagnostic
+ /// within the file where the problem occured.
+ ///
+ /// \param Loc Should be a file location, it is not meaningful for a macro
+ /// location.
+ ///
+ DiagnosticMessage(llvm::StringRef Message, const SourceManager &Sources,
+ SourceLocation Loc);
+ std::string Message;
+ std::string FilePath;
+ unsigned FileOffset;
+};
+
+/// \brief Represents the diagnostic with the level of severity and possible
+/// fixes to be applied.
+struct Diagnostic {
+ enum Level {
+ Warning = DiagnosticsEngine::Warning,
+ Error = DiagnosticsEngine::Error
+ };
+
+ Diagnostic() = default;
+
+ Diagnostic(llvm::StringRef DiagnosticName, Level DiagLevel,
+ StringRef BuildDirectory);
+
+ Diagnostic(llvm::StringRef DiagnosticName, DiagnosticMessage &Message,
+ llvm::StringMap<Replacements> &Fix,
+ SmallVector<DiagnosticMessage, 1> &Notes, Level DiagLevel,
+ llvm::StringRef BuildDirectory);
+
+ /// \brief Name identifying the Diagnostic.
+ std::string DiagnosticName;
+
+ /// \brief Message associated to the diagnostic.
+ DiagnosticMessage Message;
+
+ /// \brief Fixes to apply, grouped by file path.
+ llvm::StringMap<Replacements> Fix;
+
+ /// \brief Potential notes about the diagnostic.
+ SmallVector<DiagnosticMessage, 1> Notes;
+
+ /// \brief Diagnostic level. Can indicate either an error or a warning.
+ Level DiagLevel;
+
+ /// \brief A build directory of the diagnostic source file.
+ ///
+ /// It's an absolute path which is `directory` field of the source file in
+ /// compilation database. If users don't specify the compilation database
+ /// directory, it is the current directory where clang-tidy runs.
+ ///
+ /// Note: it is empty in unittest.
+ std::string BuildDirectory;
+};
+
+/// \brief Collection of Diagnostics generated from a single translation unit.
+struct TranslationUnitDiagnostics {
+ /// Name of the main source for the translation unit.
+ std::string MainSourceFile;
+ std::vector<Diagnostic> Diagnostics;
+};
+
+} // end namespace tooling
+} // end namespace clang
+#endif // LLVM_CLANG_TOOLING_CORE_DIAGNOSTIC_H
Modified: cfe/trunk/include/clang/Tooling/Core/Replacement.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Core/Replacement.h?rev=290892&r1=290891&r2=290892&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/Core/Replacement.h (original)
+++ cfe/trunk/include/clang/Tooling/Core/Replacement.h Tue Jan 3 08:35:47 2017
@@ -329,12 +329,6 @@ llvm::Expected<std::string> applyAllRepl
struct TranslationUnitReplacements {
/// Name of the main source for the translation unit.
std::string MainSourceFile;
-
- /// A freeform chunk of text to describe the context of the replacements.
- /// Will be printed, for example, when detecting conflicts during replacement
- /// deduplication.
- std::string Context;
-
std::vector<Replacement> Replacements;
};
Added: cfe/trunk/include/clang/Tooling/DiagnosticsYaml.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/DiagnosticsYaml.h?rev=290892&view=auto
==============================================================================
--- cfe/trunk/include/clang/Tooling/DiagnosticsYaml.h (added)
+++ cfe/trunk/include/clang/Tooling/DiagnosticsYaml.h Tue Jan 3 08:35:47 2017
@@ -0,0 +1,101 @@
+//===-- DiagnosticsYaml.h -- Serialiazation for Diagnosticss ---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief This file defines the structure of a YAML document for serializing
+/// diagnostics.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
+#define LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
+
+#include "clang/Tooling/Core/Diagnostic.h"
+#include "clang/Tooling/ReplacementsYaml.h"
+#include "llvm/Support/YAMLTraits.h"
+#include <string>
+
+LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Diagnostic)
+
+namespace llvm {
+namespace yaml {
+
+template <> struct MappingTraits<clang::tooling::Diagnostic> {
+ /// \brief Helper to (de)serialize a Diagnostic since we don't have direct
+ /// access to its data members.
+ class NormalizedDiagnostic {
+ public:
+ NormalizedDiagnostic(const IO &)
+ : DiagLevel(clang::tooling::Diagnostic::Level::Warning) {}
+
+ NormalizedDiagnostic(const IO &, const clang::tooling::Diagnostic &D)
+ : DiagnosticName(D.DiagnosticName), Message(D.Message), Fix(D.Fix),
+ Notes(D.Notes), DiagLevel(D.DiagLevel),
+ BuildDirectory(D.BuildDirectory) {}
+
+ clang::tooling::Diagnostic denormalize(const IO &) {
+ return clang::tooling::Diagnostic(DiagnosticName, Message, Fix, Notes,
+ DiagLevel, BuildDirectory);
+ }
+
+ std::string DiagnosticName;
+ clang::tooling::DiagnosticMessage Message;
+ llvm::StringMap<clang::tooling::Replacements> Fix;
+ SmallVector<clang::tooling::DiagnosticMessage, 1> Notes;
+ clang::tooling::Diagnostic::Level DiagLevel;
+ std::string BuildDirectory;
+ };
+
+ static void mapping(IO &Io, clang::tooling::Diagnostic &D) {
+ MappingNormalization<NormalizedDiagnostic, clang::tooling::Diagnostic> Keys(
+ Io, D);
+ Io.mapRequired("DiagnosticName", Keys->DiagnosticName);
+
+ // FIXME: Export properly all the different fields.
+
+ std::vector<clang::tooling::Replacement> Fixes;
+ for (auto &Replacements : Keys->Fix) {
+ for (auto &Replacement : Replacements.second) {
+ Fixes.push_back(Replacement);
+ }
+ }
+ Io.mapRequired("Replacements", Fixes);
+ for (auto &Fix : Fixes) {
+ llvm::Error Err = Keys->Fix[Fix.getFilePath()].add(Fix);
+ if (Err) {
+ // FIXME: Implement better conflict handling.
+ llvm::errs() << "Fix conflicts with existing fix: "
+ << llvm::toString(std::move(Err)) << "\n";
+ }
+ }
+ }
+};
+
+/// \brief Specialized MappingTraits to describe how a
+/// TranslationUnitDiagnostics is (de)serialized.
+template <> struct MappingTraits<clang::tooling::TranslationUnitDiagnostics> {
+ static void mapping(IO &Io, clang::tooling::TranslationUnitDiagnostics &Doc) {
+ Io.mapRequired("MainSourceFile", Doc.MainSourceFile);
+
+ std::vector<clang::tooling::Diagnostic> Diagnostics;
+ for (auto &Diagnostic : Doc.Diagnostics) {
+ // FIXME: Export all diagnostics, not just the ones with fixes.
+ // Update MappingTraits<clang::tooling::Diagnostic>::mapping.
+ if (Diagnostic.Fix.size() > 0) {
+ Diagnostics.push_back(Diagnostic);
+ }
+ }
+ Io.mapRequired("Diagnostics", Diagnostics);
+ Doc.Diagnostics = Diagnostics;
+ }
+};
+} // end namespace yaml
+} // end namespace llvm
+
+#endif // LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
Modified: cfe/trunk/include/clang/Tooling/ReplacementsYaml.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/ReplacementsYaml.h?rev=290892&r1=290891&r2=290892&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/ReplacementsYaml.h (original)
+++ cfe/trunk/include/clang/Tooling/ReplacementsYaml.h Tue Jan 3 08:35:47 2017
@@ -65,7 +65,6 @@ template <> struct MappingTraits<clang::
static void mapping(IO &Io,
clang::tooling::TranslationUnitReplacements &Doc) {
Io.mapRequired("MainSourceFile", Doc.MainSourceFile);
- Io.mapOptional("Context", Doc.Context, std::string());
Io.mapRequired("Replacements", Doc.Replacements);
}
};
Modified: cfe/trunk/lib/Tooling/Core/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Core/CMakeLists.txt?rev=290892&r1=290891&r2=290892&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/Core/CMakeLists.txt (original)
+++ cfe/trunk/lib/Tooling/Core/CMakeLists.txt Tue Jan 3 08:35:47 2017
@@ -4,6 +4,7 @@ add_clang_library(clangToolingCore
Lookup.cpp
Replacement.cpp
QualTypeNames.cpp
+ Diagnostic.cpp
LINK_LIBS
clangAST
Added: cfe/trunk/lib/Tooling/Core/Diagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Core/Diagnostic.cpp?rev=290892&view=auto
==============================================================================
--- cfe/trunk/lib/Tooling/Core/Diagnostic.cpp (added)
+++ cfe/trunk/lib/Tooling/Core/Diagnostic.cpp Tue Jan 3 08:35:47 2017
@@ -0,0 +1,46 @@
+//===--- Diagnostic.cpp - Framework for clang diagnostics tools ----------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Implements classes to support/store diagnostics refactoring.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Tooling/Core/Diagnostic.h"
+#include "clang/Basic/SourceManager.h"
+
+namespace clang {
+namespace tooling {
+
+DiagnosticMessage::DiagnosticMessage(llvm::StringRef Message)
+ : Message(Message), FileOffset(0) {}
+
+DiagnosticMessage::DiagnosticMessage(llvm::StringRef Message,
+ const SourceManager &Sources,
+ SourceLocation Loc)
+ : Message(Message) {
+ assert(Loc.isValid() && Loc.isFileID());
+ FilePath = Sources.getFilename(Loc);
+ FileOffset = Sources.getFileOffset(Loc);
+}
+
+Diagnostic::Diagnostic(llvm::StringRef DiagnosticName,
+ Diagnostic::Level DiagLevel, StringRef BuildDirectory)
+ : DiagnosticName(DiagnosticName), DiagLevel(DiagLevel),
+ BuildDirectory(BuildDirectory) {}
+
+Diagnostic::Diagnostic(llvm::StringRef DiagnosticName,
+ DiagnosticMessage &Message,
+ llvm::StringMap<Replacements> &Fix,
+ SmallVector<DiagnosticMessage, 1> &Notes,
+ Level DiagLevel, llvm::StringRef BuildDirectory)
+ : DiagnosticName(DiagnosticName), Message(Message), Fix(Fix), Notes(Notes),
+ DiagLevel(DiagLevel), BuildDirectory(BuildDirectory) {}
+
+} // end namespace tooling
+} // end namespace clang
Modified: cfe/trunk/unittests/Tooling/ReplacementsYamlTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/ReplacementsYamlTest.cpp?rev=290892&r1=290891&r2=290892&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/ReplacementsYamlTest.cpp (original)
+++ cfe/trunk/unittests/Tooling/ReplacementsYamlTest.cpp Tue Jan 3 08:35:47 2017
@@ -22,11 +22,8 @@ TEST(ReplacementsYamlTest, serializesRep
TranslationUnitReplacements Doc;
Doc.MainSourceFile = "/path/to/source.cpp";
- Doc.Context = "some context";
- Doc.Replacements
- .push_back(Replacement("/path/to/file1.h", 232, 56, "replacement #1"));
- Doc.Replacements
- .push_back(Replacement("/path/to/file2.h", 301, 2, "replacement #2"));
+ Doc.Replacements.emplace_back("/path/to/file1.h", 232, 56, "replacement #1");
+ Doc.Replacements.emplace_back("/path/to/file2.h", 301, 2, "replacement #2");
std::string YamlContent;
llvm::raw_string_ostream YamlContentStream(YamlContent);
@@ -37,7 +34,6 @@ TEST(ReplacementsYamlTest, serializesRep
// NOTE: If this test starts to fail for no obvious reason, check whitespace.
ASSERT_STREQ("---\n"
"MainSourceFile: /path/to/source.cpp\n"
- "Context: some context\n"
"Replacements: \n" // Extra whitespace here!
" - FilePath: /path/to/file1.h\n"
" Offset: 232\n"
@@ -54,7 +50,6 @@ TEST(ReplacementsYamlTest, serializesRep
TEST(ReplacementsYamlTest, deserializesReplacements) {
std::string YamlContent = "---\n"
"MainSourceFile: /path/to/source.cpp\n"
- "Context: some context\n"
"Replacements:\n"
" - FilePath: /path/to/file1.h\n"
" Offset: 232\n"
@@ -71,7 +66,6 @@ TEST(ReplacementsYamlTest, deserializesR
ASSERT_FALSE(YAML.error());
ASSERT_EQ(2u, DocActual.Replacements.size());
ASSERT_EQ("/path/to/source.cpp", DocActual.MainSourceFile);
- ASSERT_EQ("some context", DocActual.Context);
ASSERT_EQ("/path/to/file1.h", DocActual.Replacements[0].getFilePath());
ASSERT_EQ(232u, DocActual.Replacements[0].getOffset());
ASSERT_EQ(56u, DocActual.Replacements[0].getLength());
@@ -98,7 +92,6 @@ TEST(ReplacementsYamlTest, deserializesW
ASSERT_FALSE(YAML.error());
ASSERT_EQ("/path/to/source.cpp", DocActual.MainSourceFile);
ASSERT_EQ(1u, DocActual.Replacements.size());
- ASSERT_EQ(std::string(), DocActual.Context);
ASSERT_EQ("target_file.h", DocActual.Replacements[0].getFilePath());
ASSERT_EQ(1u, DocActual.Replacements[0].getOffset());
ASSERT_EQ(10u, DocActual.Replacements[0].getLength());
More information about the cfe-commits
mailing list