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