[clang-tidy] Generate valid JSON for characters that require escaping (#187454)

As of AI Usage: This PR is assisted by Claude
Closes #187201
This commit is contained in:
Zeyi Xu
2026-03-20 23:55:00 +08:00
committed by GitHub
parent 8cbf3f3c39
commit 6d45f6dfb7
3 changed files with 24 additions and 7 deletions

View File

@@ -9,6 +9,7 @@
#include "ClangTidyProfiling.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <optional>
@@ -45,13 +46,24 @@ void ClangTidyProfiling::printUserFriendlyTable(llvm::raw_ostream &OS,
void ClangTidyProfiling::printAsJSON(llvm::raw_ostream &OS,
llvm::TimerGroup &TG) {
assert(Storage && "We should have a filename.");
OS << "{\n";
OS << R"("file": ")" << Storage->SourceFilename << "\",\n";
OS << R"("timestamp": ")" << Storage->Timestamp << "\",\n";
OS << "\"profile\": {\n";
TG.printJSONValues(OS, "");
OS << "\n}\n";
OS << "}\n";
std::string TimestampStr;
llvm::raw_string_ostream TmpOS(TimestampStr);
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
TmpOS << Storage->Timestamp;
llvm::json::OStream JOS(OS, 2);
JOS.object([&] {
JOS.attribute("file", Storage->SourceFilename);
JOS.attribute("timestamp", TimestampStr);
JOS.attributeBegin("profile");
JOS.rawValue([&](llvm::raw_ostream &ROS) {
ROS << "{\n";
TG.printJSONValues(ROS, "");
ROS << "\n}";
});
JOS.attributeEnd();
});
OS << "\n";
OS.flush();
}

View File

@@ -102,6 +102,9 @@ Improvements to clang-tidy
manages the creation of temporary header files and ensures that diagnostics
and fixes are verified for the specified headers.
- Improved :program:`clang-tidy` ``-store-check-profile`` by generating valid
JSON when the source file path contains characters that require JSON escaping.
New checks
^^^^^^^^^^

View File

@@ -1,9 +1,11 @@
// RUN: rm -rf %t.dir/out
// RUN: clang-tidy -enable-check-profile -checks='-*,readability-function-size' -store-check-profile=%t.dir/out %s -- 2>&1 | not FileCheck --match-full-lines -implicit-check-not='{{warning:|error:}}' -check-prefix=CHECK-CONSOLE %s
// RUN: cat %t.dir/out/*-clang-tidy-store-check-profile-one-tu.cpp.json | FileCheck --match-full-lines -implicit-check-not='{{warning:|error:}}' -check-prefix=CHECK-FILE %s
// RUN: cat %t.dir/out/*-clang-tidy-store-check-profile-one-tu.cpp.json | %python -c "import sys, json; json.load(sys.stdin)"
// RUN: rm -rf %t.dir/out
// RUN: clang-tidy -enable-check-profile -checks='-*,readability-function-size' -store-check-profile=%t.dir/out %s -- 2>&1
// RUN: cat %t.dir/out/*-clang-tidy-store-check-profile-one-tu.cpp.json | FileCheck --match-full-lines -implicit-check-not='{{warning:|error:}}' -check-prefix=CHECK-FILE %s
// RUN: cat %t.dir/out/*-clang-tidy-store-check-profile-one-tu.cpp.json | %python -c "import sys, json; json.load(sys.stdin)"
// CHECK-CONSOLE-NOT: ===-------------------------------------------------------------------------===
// CHECK-CONSOLE-NOT: {{.*}} --- Name ---