Skip to content

[clang-tidy][NFC] simplify TimerGroup in ClangTidyProfiling #123958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

HerrCai0907
Copy link
Contributor

TimerGroup don't need to use as field of ClangTidyProfiling.
We can construct it local during destructing.

`TimerGroup` don't need to use as field of `ClangTidyProfiling`.
We can construct it local during destructing.
@llvmbot
Copy link
Member

llvmbot commented Jan 22, 2025

@llvm/pr-subscribers-clang-tidy

Author: Congcong Cai (HerrCai0907)

Changes

TimerGroup don't need to use as field of ClangTidyProfiling.
We can construct it local during destructing.


Full diff: https://github.com/llvm/llvm-project/pull/123958.diff

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp (+11-10)
  • (modified) clang-tools-extra/clang-tidy/ClangTidyProfiling.h (+3-6)
diff --git a/clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp b/clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp
index 07ab34a07cd31d..89867ec30f51f3 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp
@@ -36,23 +36,25 @@ ClangTidyProfiling::StorageParams::StorageParams(llvm::StringRef ProfilePrefix,
                       .str();
 }
 
-void ClangTidyProfiling::printUserFriendlyTable(llvm::raw_ostream &OS) {
-  TG->print(OS);
+void ClangTidyProfiling::printUserFriendlyTable(llvm::raw_ostream &OS,
+                                                llvm::TimerGroup &TG) {
+  TG.print(OS);
   OS.flush();
 }
 
-void ClangTidyProfiling::printAsJSON(llvm::raw_ostream &OS) {
+void ClangTidyProfiling::printAsJSON(llvm::raw_ostream &OS,
+                                     llvm::TimerGroup &TG) {
   OS << "{\n";
   OS << R"("file": ")" << Storage->SourceFilename << "\",\n";
   OS << R"("timestamp": ")" << Storage->Timestamp << "\",\n";
   OS << "\"profile\": {\n";
-  TG->printJSONValues(OS, "");
+  TG.printJSONValues(OS, "");
   OS << "\n}\n";
   OS << "}\n";
   OS.flush();
 }
 
-void ClangTidyProfiling::storeProfileData() {
+void ClangTidyProfiling::storeProfileData(llvm::TimerGroup &TG) {
   assert(Storage && "We should have a filename.");
 
   llvm::SmallString<256> OutputDirectory(Storage->StoreFilename);
@@ -71,19 +73,18 @@ void ClangTidyProfiling::storeProfileData() {
     return;
   }
 
-  printAsJSON(OS);
+  printAsJSON(OS, TG);
 }
 
 ClangTidyProfiling::ClangTidyProfiling(std::optional<StorageParams> Storage)
     : Storage(std::move(Storage)) {}
 
 ClangTidyProfiling::~ClangTidyProfiling() {
-  TG.emplace("clang-tidy", "clang-tidy checks profiling", Records);
-
+  llvm::TimerGroup TG{"clang-tidy", "clang-tidy checks profiling", Records};
   if (!Storage)
-    printUserFriendlyTable(llvm::errs());
+    printUserFriendlyTable(llvm::errs(), TG);
   else
-    storeProfileData();
+    storeProfileData(TG);
 }
 
 } // namespace clang::tidy
diff --git a/clang-tools-extra/clang-tidy/ClangTidyProfiling.h b/clang-tools-extra/clang-tidy/ClangTidyProfiling.h
index b6f7d66343fa46..76deede1716f44 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyProfiling.h
+++ b/clang-tools-extra/clang-tidy/ClangTidyProfiling.h
@@ -34,14 +34,11 @@ class ClangTidyProfiling {
   };
 
 private:
-  std::optional<llvm::TimerGroup> TG;
-
   std::optional<StorageParams> Storage;
 
-  void printUserFriendlyTable(llvm::raw_ostream &OS);
-  void printAsJSON(llvm::raw_ostream &OS);
-
-  void storeProfileData();
+  void printUserFriendlyTable(llvm::raw_ostream &OS, llvm::TimerGroup &TG);
+  void printAsJSON(llvm::raw_ostream &OS, llvm::TimerGroup &TG);
+  void storeProfileData(llvm::TimerGroup &TG);
 
 public:
   llvm::StringMap<llvm::TimeRecord> Records;

@llvmbot
Copy link
Member

llvmbot commented Jan 22, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Congcong Cai (HerrCai0907)

Changes

TimerGroup don't need to use as field of ClangTidyProfiling.
We can construct it local during destructing.


Full diff: https://github.com/llvm/llvm-project/pull/123958.diff

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp (+11-10)
  • (modified) clang-tools-extra/clang-tidy/ClangTidyProfiling.h (+3-6)
diff --git a/clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp b/clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp
index 07ab34a07cd31d..89867ec30f51f3 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp
@@ -36,23 +36,25 @@ ClangTidyProfiling::StorageParams::StorageParams(llvm::StringRef ProfilePrefix,
                       .str();
 }
 
-void ClangTidyProfiling::printUserFriendlyTable(llvm::raw_ostream &OS) {
-  TG->print(OS);
+void ClangTidyProfiling::printUserFriendlyTable(llvm::raw_ostream &OS,
+                                                llvm::TimerGroup &TG) {
+  TG.print(OS);
   OS.flush();
 }
 
-void ClangTidyProfiling::printAsJSON(llvm::raw_ostream &OS) {
+void ClangTidyProfiling::printAsJSON(llvm::raw_ostream &OS,
+                                     llvm::TimerGroup &TG) {
   OS << "{\n";
   OS << R"("file": ")" << Storage->SourceFilename << "\",\n";
   OS << R"("timestamp": ")" << Storage->Timestamp << "\",\n";
   OS << "\"profile\": {\n";
-  TG->printJSONValues(OS, "");
+  TG.printJSONValues(OS, "");
   OS << "\n}\n";
   OS << "}\n";
   OS.flush();
 }
 
-void ClangTidyProfiling::storeProfileData() {
+void ClangTidyProfiling::storeProfileData(llvm::TimerGroup &TG) {
   assert(Storage && "We should have a filename.");
 
   llvm::SmallString<256> OutputDirectory(Storage->StoreFilename);
@@ -71,19 +73,18 @@ void ClangTidyProfiling::storeProfileData() {
     return;
   }
 
-  printAsJSON(OS);
+  printAsJSON(OS, TG);
 }
 
 ClangTidyProfiling::ClangTidyProfiling(std::optional<StorageParams> Storage)
     : Storage(std::move(Storage)) {}
 
 ClangTidyProfiling::~ClangTidyProfiling() {
-  TG.emplace("clang-tidy", "clang-tidy checks profiling", Records);
-
+  llvm::TimerGroup TG{"clang-tidy", "clang-tidy checks profiling", Records};
   if (!Storage)
-    printUserFriendlyTable(llvm::errs());
+    printUserFriendlyTable(llvm::errs(), TG);
   else
-    storeProfileData();
+    storeProfileData(TG);
 }
 
 } // namespace clang::tidy
diff --git a/clang-tools-extra/clang-tidy/ClangTidyProfiling.h b/clang-tools-extra/clang-tidy/ClangTidyProfiling.h
index b6f7d66343fa46..76deede1716f44 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyProfiling.h
+++ b/clang-tools-extra/clang-tidy/ClangTidyProfiling.h
@@ -34,14 +34,11 @@ class ClangTidyProfiling {
   };
 
 private:
-  std::optional<llvm::TimerGroup> TG;
-
   std::optional<StorageParams> Storage;
 
-  void printUserFriendlyTable(llvm::raw_ostream &OS);
-  void printAsJSON(llvm::raw_ostream &OS);
-
-  void storeProfileData();
+  void printUserFriendlyTable(llvm::raw_ostream &OS, llvm::TimerGroup &TG);
+  void printAsJSON(llvm::raw_ostream &OS, llvm::TimerGroup &TG);
+  void storeProfileData(llvm::TimerGroup &TG);
 
 public:
   llvm::StringMap<llvm::TimeRecord> Records;

@HerrCai0907 HerrCai0907 merged commit 46a08ce into llvm:main Jan 24, 2025
11 checks passed
@HerrCai0907 HerrCai0907 deleted the clang-tidy/remove-tg-in-check-profile branch January 24, 2025 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants