10#include "llvm/ADT/SmallString.h"
11#include "llvm/Support/FileSystem.h"
12#include "llvm/Support/JSON.h"
13#include "llvm/Support/Path.h"
14#include "llvm/Support/raw_ostream.h"
16#include <system_error>
19#define DEBUG_TYPE "clang-tidy-profiling"
24 llvm::StringRef SourceFile)
26 llvm::SmallString<32> TimestampStr;
27 llvm::raw_svector_ostream OS(TimestampStr);
31 llvm::SmallString<256> FinalPrefix(ProfilePrefix);
32 llvm::sys::path::append(FinalPrefix, TimestampStr);
36 llvm::sys::path::filename(SourceFile) +
".json")
40void ClangTidyProfiling::printUserFriendlyTable(llvm::raw_ostream &OS,
41 llvm::TimerGroup &TG) {
46void ClangTidyProfiling::printAsJSON(llvm::raw_ostream &OS,
47 llvm::TimerGroup &TG) {
48 assert(Storage &&
"We should have a filename.");
49 std::string TimestampStr;
50 llvm::raw_string_ostream TmpOS(TimestampStr);
52 TmpOS << Storage->Timestamp;
54 llvm::json::OStream JOS(OS, 2);
56 JOS.attribute(
"file", Storage->SourceFilename);
57 JOS.attribute(
"timestamp", TimestampStr);
58 JOS.attributeBegin(
"profile");
59 JOS.rawValue([&](llvm::raw_ostream &ROS) {
61 TG.printJSONValues(ROS,
"");
70void ClangTidyProfiling::storeProfileData(llvm::TimerGroup &TG) {
71 assert(Storage &&
"We should have a filename.");
72 llvm::SmallString<256> OutputDirectory(Storage->StoreFilename);
73 llvm::sys::path::remove_filename(OutputDirectory);
74 if (
const std::error_code EC =
75 llvm::sys::fs::create_directories(OutputDirectory)) {
76 llvm::errs() <<
"Unable to create output directory '" << OutputDirectory
77 <<
"': " << EC.message() <<
"\n";
82 llvm::raw_fd_ostream OS(Storage->StoreFilename, EC, llvm::sys::fs::OF_None);
84 llvm::errs() <<
"Error opening output file '" << Storage->StoreFilename
85 <<
"': " << EC.message() <<
"\n";
93 : Storage(std::
move(Storage)) {}
96 llvm::TimerGroup TG{
"clang-tidy",
"clang-tidy checks profiling",
Records};
98 printUserFriendlyTable(llvm::errs(), TG);
100 storeProfileData(TG);
llvm::StringMap< llvm::TimeRecord > Records
ClangTidyProfiling()=default
std::string StoreFilename
llvm::sys::TimePoint Timestamp
std::string SourceFilename