Skip to content

[Clang] Check PP presence when printing stats #131608

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
merged 2 commits into from
Mar 28, 2025

Conversation

ecnelises
Copy link
Member

@ecnelises ecnelises commented Mar 17, 2025

Front-end option -print-stats can be used to print statistics around the compilation process. But clang with this options will crash when input is IR file. This patch fixes the crash by checking preprocessor presence before invoking it.

@ecnelises ecnelises added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Mar 17, 2025
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Mar 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 17, 2025

@llvm/pr-subscribers-clang

Author: Qiu Chaofan (ecnelises)

Changes

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

2 Files Affected:

  • (modified) clang/lib/Frontend/FrontendAction.cpp (+8-4)
  • (added) clang/test/Frontend/print-stats.ll (+6)
diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp
index 9f789f093f55d..a750953393759 100644
--- a/clang/lib/Frontend/FrontendAction.cpp
+++ b/clang/lib/Frontend/FrontendAction.cpp
@@ -1120,10 +1120,14 @@ void FrontendAction::EndSourceFile() {
 
   if (CI.getFrontendOpts().ShowStats) {
     llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFileOrBufferName() << "':\n";
-    CI.getPreprocessor().PrintStats();
-    CI.getPreprocessor().getIdentifierTable().PrintStats();
-    CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
-    CI.getSourceManager().PrintStats();
+    if (CI.hasPreprocessor()) {
+      CI.getPreprocessor().PrintStats();
+      CI.getPreprocessor().getIdentifierTable().PrintStats();
+      CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
+    }
+    if (CI.hasSourceManager()) {
+      CI.getSourceManager().PrintStats();
+    }
     llvm::errs() << "\n";
   }
 
diff --git a/clang/test/Frontend/print-stats.ll b/clang/test/Frontend/print-stats.ll
new file mode 100644
index 0000000000000..6fd094470ba95
--- /dev/null
+++ b/clang/test/Frontend/print-stats.ll
@@ -0,0 +1,6 @@
+; RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -print-stats \
+; RUN:    -emit-llvm -x ir %s -o - 2>&1 | FileCheck %s
+
+; CHECK: *** Source Manager Stats
+; CHECK: *** File Manager Stats
+; CHECK: *** Virtual File System Stats

@yronglin
Copy link
Contributor

Thanks working on this! Can you add some description for this patch?

Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix!

Second, that we need a summary explaining the problem and solution.

Is this linked to a bug report, if it is, then we should mention that bug report as well.

I would also like to see a test showing a case where we successfully print the preprocessor stats as well, if we don't already have one.

@ecnelises ecnelises requested a review from shafik March 24, 2025 12:23
@cor3ntin
Copy link
Contributor

This change needs a release note.
Please add an entry to clang/docs/ReleaseNotes.rst in the section the most adapted to the change, and referencing any Github issue this change fixes. Thanks!

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Will you need me to merge this for you?

@ecnelises ecnelises merged commit b3f01a6 into llvm:main Mar 28, 2025
12 checks passed
@ecnelises ecnelises deleted the fix_pp_presence branch March 28, 2025 17:54
@ecnelises
Copy link
Member Author

Done, thanks!

@MaskRay
Copy link
Member

MaskRay commented Mar 29, 2025

LGTM! Thanks for the fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants