clang-tools  11.0.0
ClangTidyProfiling.cpp
Go to the documentation of this file.
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/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>
17 #include <utility>
18 
19 #define DEBUG_TYPE "clang-tidy-profiling"
20 
21 namespace clang {
22 namespace tidy {
23 
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);
29  llvm::format_provider<decltype(Timestamp)>::format(Timestamp, OS,
30  "%Y%m%d%H%M%S%N");
31 
32  llvm::SmallString<256> FinalPrefix(ProfilePrefix);
33  llvm::sys::path::append(FinalPrefix, TimestampStr);
34 
35  // So the full output name is: /ProfilePrefix/timestamp-inputfilename.json
36  StoreFilename = llvm::Twine(FinalPrefix + "-" +
37  llvm::sys::path::filename(SourceFile) + ".json")
38  .str();
39 }
40 
41 void ClangTidyProfiling::printUserFriendlyTable(llvm::raw_ostream &OS) {
42  TG->print(OS);
43  OS.flush();
44 }
45 
46 void ClangTidyProfiling::printAsJSON(llvm::raw_ostream &OS) {
47  OS << "{\n";
48  OS << "\"file\": \"" << Storage->SourceFilename << "\",\n";
49  OS << "\"timestamp\": \"" << Storage->Timestamp << "\",\n";
50  OS << "\"profile\": {\n";
51  TG->printJSONValues(OS, "");
52  OS << "\n}\n";
53  OS << "}\n";
54  OS.flush();
55 }
56 
57 void ClangTidyProfiling::storeProfileData() {
58  assert(Storage.hasValue() && "We should have a filename.");
59 
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";
65  return;
66  }
67 
68  std::error_code EC;
69  llvm::raw_fd_ostream OS(Storage->StoreFilename, EC, llvm::sys::fs::OF_None);
70  if (EC) {
71  llvm::errs() << "Error opening output file '" << Storage->StoreFilename
72  << "': " << EC.message() << "\n";
73  return;
74  }
75 
76  printAsJSON(OS);
77 }
78 
79 ClangTidyProfiling::ClangTidyProfiling(llvm::Optional<StorageParams> Storage)
80  : Storage(std::move(Storage)) {}
81 
83  TG.emplace("clang-tidy", "clang-tidy checks profiling", Records);
84 
85  if (!Storage.hasValue())
86  printUserFriendlyTable(llvm::errs());
87  else
88  storeProfileData();
89 }
90 
91 } // namespace tidy
92 } // namespace clang
clang::tidy::ClangTidyProfiling::Records
llvm::StringMap< llvm::TimeRecord > Records
Definition: ClangTidyProfiling.h:48
clang::tidy::ClangTidyProfiling::StorageParams::StorageParams
StorageParams()=default
clang::tidy::ClangTidyProfiling::StorageParams::StoreFilename
std::string StoreFilename
Definition: ClangTidyProfiling.h:30
clang::tidy::ClangTidyProfiling::StorageParams::Timestamp
llvm::sys::TimePoint Timestamp
Definition: ClangTidyProfiling.h:28
clang::tidy::ClangTidyProfiling::ClangTidyProfiling
ClangTidyProfiling()=default
clang::tidy::ClangTidyProfiling::~ClangTidyProfiling
~ClangTidyProfiling()
Definition: ClangTidyProfiling.cpp:82
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:162
ClangTidyProfiling.h