clang-tools 23.0.0git
ClangTidyProfiling.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
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"
15#include <optional>
16#include <system_error>
17#include <utility>
18
19#define DEBUG_TYPE "clang-tidy-profiling"
20
21namespace clang::tidy {
22
24 llvm::StringRef SourceFile)
25 : Timestamp(std::chrono::system_clock::now()), SourceFilename(SourceFile) {
26 llvm::SmallString<32> TimestampStr;
27 llvm::raw_svector_ostream OS(TimestampStr);
28 llvm::format_provider<decltype(Timestamp)>::format(Timestamp, OS,
29 "%Y%m%d%H%M%S%N");
30
31 llvm::SmallString<256> FinalPrefix(ProfilePrefix);
32 llvm::sys::path::append(FinalPrefix, TimestampStr);
33
34 // So the full output name is: /ProfilePrefix/timestamp-inputfilename.json
35 StoreFilename = llvm::Twine(FinalPrefix + "-" +
36 llvm::sys::path::filename(SourceFile) + ".json")
37 .str();
38}
39
40void ClangTidyProfiling::printUserFriendlyTable(llvm::raw_ostream &OS,
41 llvm::TimerGroup &TG) {
42 TG.print(OS);
43 OS.flush();
44}
45
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);
51 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
52 TmpOS << Storage->Timestamp;
53
54 llvm::json::OStream JOS(OS, 2);
55 JOS.object([&] {
56 JOS.attribute("file", Storage->SourceFilename);
57 JOS.attribute("timestamp", TimestampStr);
58 JOS.attributeBegin("profile");
59 JOS.rawValue([&](llvm::raw_ostream &ROS) {
60 ROS << "{\n";
61 TG.printJSONValues(ROS, "");
62 ROS << "\n}";
63 });
64 JOS.attributeEnd();
65 });
66 OS << "\n";
67 OS.flush();
68}
69
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";
78 return;
79 }
80
81 std::error_code EC;
82 llvm::raw_fd_ostream OS(Storage->StoreFilename, EC, llvm::sys::fs::OF_None);
83 if (EC) {
84 llvm::errs() << "Error opening output file '" << Storage->StoreFilename
85 << "': " << EC.message() << "\n";
86 return;
87 }
88
89 printAsJSON(OS, TG);
90}
91
92ClangTidyProfiling::ClangTidyProfiling(std::optional<StorageParams> Storage)
93 : Storage(std::move(Storage)) {}
94
96 llvm::TimerGroup TG{"clang-tidy", "clang-tidy checks profiling", Records};
97 if (!Storage)
98 printUserFriendlyTable(llvm::errs(), TG);
99 else
100 storeProfileData(TG);
101}
102
103} // namespace clang::tidy
llvm::StringMap< llvm::TimeRecord > Records