1 //===--- ClangTidyProfiling.cpp - clang-tidy --------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ClangTidyProfiling.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/Support/FileSystem.h"
12 #include "llvm/Support/Path.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include <system_error>
15 #include <utility>
16 
17 #define DEBUG_TYPE "clang-tidy-profiling"
18 
19 namespace clang {
20 namespace tidy {
21 
StorageParams(llvm::StringRef ProfilePrefix,llvm::StringRef SourceFile)22 ClangTidyProfiling::StorageParams::StorageParams(llvm::StringRef ProfilePrefix,
23                                                  llvm::StringRef SourceFile)
24     : Timestamp(std::chrono::system_clock::now()), SourceFilename(SourceFile) {
25   llvm::SmallString<32> TimestampStr;
26   llvm::raw_svector_ostream OS(TimestampStr);
27   llvm::format_provider<decltype(Timestamp)>::format(Timestamp, OS,
28                                                      "%Y%m%d%H%M%S%N");
29 
30   llvm::SmallString<256> FinalPrefix(ProfilePrefix);
31   llvm::sys::path::append(FinalPrefix, TimestampStr);
32 
33   // So the full output name is: /ProfilePrefix/timestamp-inputfilename.json
34   StoreFilename = llvm::Twine(FinalPrefix + "-" +
35                               llvm::sys::path::filename(SourceFile) + ".json")
36                       .str();
37 }
38 
printUserFriendlyTable(llvm::raw_ostream & OS)39 void ClangTidyProfiling::printUserFriendlyTable(llvm::raw_ostream &OS) {
40   TG->print(OS);
41   OS.flush();
42 }
43 
printAsJSON(llvm::raw_ostream & OS)44 void ClangTidyProfiling::printAsJSON(llvm::raw_ostream &OS) {
45   OS << "{\n";
46   OS << "\"file\": \"" << Storage->SourceFilename << "\",\n";
47   OS << "\"timestamp\": \"" << Storage->Timestamp << "\",\n";
48   OS << "\"profile\": {\n";
49   TG->printJSONValues(OS, "");
50   OS << "\n}\n";
51   OS << "}\n";
52   OS.flush();
53 }
54 
storeProfileData()55 void ClangTidyProfiling::storeProfileData() {
56   assert(Storage.hasValue() && "We should have a filename.");
57 
58   llvm::SmallString<256> OutputDirectory(Storage->StoreFilename);
59   llvm::sys::path::remove_filename(OutputDirectory);
60   if (std::error_code EC = llvm::sys::fs::create_directories(OutputDirectory)) {
61     llvm::errs() << "Unable to create output directory '" << OutputDirectory
62                  << "': " << EC.message() << "\n";
63     return;
64   }
65 
66   std::error_code EC;
67   llvm::raw_fd_ostream OS(Storage->StoreFilename, EC, llvm::sys::fs::OF_None);
68   if (EC) {
69     llvm::errs() << "Error opening output file '" << Storage->StoreFilename
70                  << "': " << EC.message() << "\n";
71     return;
72   }
73 
74   printAsJSON(OS);
75 }
76 
ClangTidyProfiling(llvm::Optional<StorageParams> Storage)77 ClangTidyProfiling::ClangTidyProfiling(llvm::Optional<StorageParams> Storage)
78     : Storage(std::move(Storage)) {}
79 
~ClangTidyProfiling()80 ClangTidyProfiling::~ClangTidyProfiling() {
81   TG.emplace("clang-tidy", "clang-tidy checks profiling", Records);
82 
83   if (!Storage.hasValue())
84     printUserFriendlyTable(llvm::errs());
85   else
86     storeProfileData();
87 }
88 
89 } // namespace tidy
90 } // namespace clang
91