[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
This commit is contained in:
Dmitry Polukhin 2020-05-02 02:35:41 -07:00
parent 046566a1d5
commit c98c94d85f
3 changed files with 33 additions and 4 deletions

View file

@ -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: ...

View file

@ -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 different 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

View file

@ -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) {