[clang] c98c94d - [clang-tidy] Add diagnostics level to YAML output

Dmitry Polukhin via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 15 07:41:52 PDT 2020


Author: Dmitry Polukhin
Date: 2020-06-15T07:40:53-07:00
New Revision: c98c94d85f8591c22f369e8f35142379ba27bb33

URL: https://github.com/llvm/llvm-project/commit/c98c94d85f8591c22f369e8f35142379ba27bb33
DIFF: https://github.com/llvm/llvm-project/commit/c98c94d85f8591c22f369e8f35142379ba27bb33.diff

LOG: [clang-tidy] Add diagnostics level to YAML output

Summary:
Also added BuildDirectory for completness and removed unused `Fix`.

Test Plan: check-all

Reviewers: alexfh, gribozavr2

Subscribers: xazax.hun, cfe-commits

Tags: #clang-tools-extra, #clang

Differential Revision: https://reviews.llvm.org/D79285

Added: 
    

Modified: 
    clang-tools-extra/test/clang-tidy/infrastructure/export-diagnostics.cpp
    clang/include/clang/Tooling/DiagnosticsYaml.h
    clang/unittests/Tooling/DiagnosticsYamlTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/test/clang-tidy/infrastructure/export-diagnostics.cpp b/clang-tools-extra/test/clang-tidy/infrastructure/export-diagnostics.cpp
index eb77be8fbdb0..13d7684597e5 100644
--- a/clang-tools-extra/test/clang-tidy/infrastructure/export-diagnostics.cpp
+++ b/clang-tools-extra/test/clang-tidy/infrastructure/export-diagnostics.cpp
@@ -1,15 +1,17 @@
 // RUN: grep -Ev "// *[A-Z-]+:" %s > %t-input.cpp
-// RUN: clang-tidy %t-input.cpp -checks='-*,google-explicit-constructor,clang-diagnostic-missing-prototypes' -export-fixes=%t.yaml -- -Wmissing-prototypes > %t.msg 2>&1
+// RUN: not clang-tidy %t-input.cpp -checks='-*,google-explicit-constructor,clang-diagnostic-missing-prototypes' -export-fixes=%t.yaml -- -Wmissing-prototypes > %t.msg 2>&1
 // RUN: FileCheck -input-file=%t.msg -check-prefix=CHECK-MESSAGES %s -implicit-check-not='{{warning|error|note}}:'
 // RUN: FileCheck -input-file=%t.yaml -check-prefix=CHECK-YAML %s
 #define X(n) void n ## n() {}
 X(f)
+int a[-1];
 
 // CHECK-MESSAGES: -input.cpp:2:1: warning: no previous prototype for function 'ff' [clang-diagnostic-missing-prototypes]
 // CHECK-MESSAGES: -input.cpp:1:19: note: expanded from macro 'X'
 // CHECK-MESSAGES: {{^}}note: expanded from here{{$}}
 // CHECK-MESSAGES: -input.cpp:2:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
 // CHECK-MESSAGES: -input.cpp:1:14: note: expanded from macro 'X'
+// CHECK-MESSAGES: -input.cpp:3:7: error: 'a' declared as an array with a negative size [clang-diagnostic-error]
 
 // CHECK-YAML: ---
 // CHECK-YAML-NEXT: MainSourceFile:  '{{.*}}-input.cpp'
@@ -42,4 +44,18 @@ X(f)
 // CHECK-YAML-NEXT:         FilePath:        '{{.*}}-input.cpp'
 // CHECK-YAML-NEXT:         FileOffset:      13
 // CHECK-YAML-NEXT:         Replacements:    []
+// CHECK-YAML-NEXT:     Level:           Warning
+// CHECK-YAML-NEXT:     BuildDirectory:  '{{.*}}'
+// CHECK-YAML-NEXT:   - DiagnosticName:  clang-diagnostic-error
+// CHECK-YAML-NEXT:     DiagnosticMessage:
+// CHECK-YAML-NEXT:       Message:         '''a'' declared as an array with a negative size'
+// CHECK-YAML-NEXT:       FilePath:        '{{.*}}-input.cpp'
+// CHECK-YAML-NEXT:       FileOffset:      41
+// CHECK-YAML-NEXT:       Replacements:    []
+// CHECK-YAML-NEXT:     Level:           Error
+// CHECK-YAML-NEXT:     BuildDirectory:  '{{.*}}'
+// CHECK-YAML-NEXT:     Ranges:
+// CHECK-YAML-NEXT:      - FilePath:        '{{.*}}-input.cpp'
+// CHECK-YAML-NEXT:         FileOffset:      41
+// CHECK-YAML-NEXT:         Length:          1
 // CHECK-YAML-NEXT: ...

diff  --git a/clang/include/clang/Tooling/DiagnosticsYaml.h b/clang/include/clang/Tooling/DiagnosticsYaml.h
index 38e49645dbb8..38fbcfc1da95 100644
--- a/clang/include/clang/Tooling/DiagnosticsYaml.h
+++ b/clang/include/clang/Tooling/DiagnosticsYaml.h
@@ -77,7 +77,6 @@ template <> struct MappingTraits<clang::tooling::Diagnostic> {
 
     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;
@@ -90,9 +89,9 @@ template <> struct MappingTraits<clang::tooling::Diagnostic> {
     Io.mapRequired("DiagnosticName", Keys->DiagnosticName);
     Io.mapRequired("DiagnosticMessage", Keys->Message);
     Io.mapOptional("Notes", Keys->Notes);
+    Io.mapOptional("Level", Keys->DiagLevel);
+    Io.mapOptional("BuildDirectory", Keys->BuildDirectory);
     Io.mapOptional("Ranges", Keys->Ranges);
-
-    // FIXME: Export properly all the 
diff erent fields.
   }
 };
 
@@ -104,6 +103,14 @@ template <> struct MappingTraits<clang::tooling::TranslationUnitDiagnostics> {
     Io.mapRequired("Diagnostics", Doc.Diagnostics);
   }
 };
+
+template <> struct ScalarEnumerationTraits<clang::tooling::Diagnostic::Level> {
+  static void enumeration(IO &IO, clang::tooling::Diagnostic::Level &Value) {
+    IO.enumCase(Value, "Warning", clang::tooling::Diagnostic::Warning);
+    IO.enumCase(Value, "Error", clang::tooling::Diagnostic::Error);
+  }
+};
+
 } // end namespace yaml
 } // end namespace llvm
 

diff  --git a/clang/unittests/Tooling/DiagnosticsYamlTest.cpp b/clang/unittests/Tooling/DiagnosticsYamlTest.cpp
index ef8f3ec45e64..acfefa505edb 100644
--- a/clang/unittests/Tooling/DiagnosticsYamlTest.cpp
+++ b/clang/unittests/Tooling/DiagnosticsYamlTest.cpp
@@ -65,6 +65,8 @@ static const char *YAMLContent =
     "          Offset:          100\n"
     "          Length:          12\n"
     "          ReplacementText: 'replacement #1'\n"
+    "    Level:           Warning\n"
+    "    BuildDirectory:  'path/to/build/directory'\n"
     "  - DiagnosticName:  'diagnostic#2'\n"
     "    DiagnosticMessage:\n"
     "      Message:         'message #2'\n"
@@ -75,6 +77,8 @@ static const char *YAMLContent =
     "          Offset:          62\n"
     "          Length:          2\n"
     "          ReplacementText: 'replacement #2'\n"
+    "    Level:           Warning\n"
+    "    BuildDirectory:  'path/to/build/directory'\n"
     "    Ranges:\n"
     "      - FilePath:        'path/to/source.cpp'\n"
     "        FileOffset:      10\n"
@@ -94,6 +98,8 @@ static const char *YAMLContent =
     "        FilePath:        'path/to/note2.cpp'\n"
     "        FileOffset:      99\n"
     "        Replacements:    []\n"
+    "    Level:           Warning\n"
+    "    BuildDirectory:  'path/to/build/directory'\n"
     "...\n";
 
 TEST(DiagnosticsYamlTest, serializesDiagnostics) {


        


More information about the cfe-commits mailing list