10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/Path.h"
14 #include "llvm/Support/YAMLTraits.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include <system_error>
19 #define DEBUG_TYPE "clang-tidy-profiling"
25 llvm::StringRef SourceFile)
26 : Timestamp(std::chrono::system_clock::now()), SourceFilename(SourceFile) {
27 llvm::SmallString<32> TimestampStr;
28 llvm::raw_svector_ostream
OS(TimestampStr);
32 llvm::SmallString<256> FinalPrefix(ProfilePrefix);
33 llvm::sys::path::append(FinalPrefix, TimestampStr);
37 llvm::sys::path::filename(SourceFile) +
".json")
41 void ClangTidyProfiling::printUserFriendlyTable(llvm::raw_ostream &
OS) {
46 void ClangTidyProfiling::printAsJSON(llvm::raw_ostream &
OS) {
48 OS <<
"\"file\": \"" << Storage->SourceFilename <<
"\",\n";
49 OS <<
"\"timestamp\": \"" << Storage->Timestamp <<
"\",\n";
50 OS <<
"\"profile\": {\n";
51 TG->printJSONValues(
OS,
"");
57 void ClangTidyProfiling::storeProfileData() {
58 assert(Storage.hasValue() &&
"We should have a filename.");
60 llvm::SmallString<256> OutputDirectory(Storage->StoreFilename);
61 llvm::sys::path::remove_filename(OutputDirectory);
62 if (std::error_code EC = llvm::sys::fs::create_directories(OutputDirectory)) {
63 llvm::errs() <<
"Unable to create output directory '" << OutputDirectory
64 <<
"': " << EC.message() <<
"\n";
69 llvm::raw_fd_ostream
OS(Storage->StoreFilename, EC, llvm::sys::fs::OF_None);
71 llvm::errs() <<
"Error opening output file '" << Storage->StoreFilename
72 <<
"': " << EC.message() <<
"\n";
80 : Storage(std::move(Storage)) {}
83 TG.emplace(
"clang-tidy",
"clang-tidy checks profiling",
Records);
85 if (!Storage.hasValue())
86 printUserFriendlyTable(llvm::errs());