Skip to content

Commit b3f01a6

Browse files
authored
[Clang] Check PP presence when printing stats (#131608)
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.
1 parent e70fe9b commit b3f01a6

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ Miscellaneous Bug Fixes
389389
Miscellaneous Clang Crashes Fixed
390390
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
391391

392+
- Fixed crash when ``-print-stats`` is enabled in compiling IR files. (#GH131608)
393+
392394
OpenACC Specific Changes
393395
------------------------
394396

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,10 +1074,14 @@ void FrontendAction::EndSourceFile() {
10741074

10751075
if (CI.getFrontendOpts().ShowStats) {
10761076
llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFileOrBufferName() << "':\n";
1077-
CI.getPreprocessor().PrintStats();
1078-
CI.getPreprocessor().getIdentifierTable().PrintStats();
1079-
CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
1080-
CI.getSourceManager().PrintStats();
1077+
if (CI.hasPreprocessor()) {
1078+
CI.getPreprocessor().PrintStats();
1079+
CI.getPreprocessor().getIdentifierTable().PrintStats();
1080+
CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
1081+
}
1082+
if (CI.hasSourceManager()) {
1083+
CI.getSourceManager().PrintStats();
1084+
}
10811085
llvm::errs() << "\n";
10821086
}
10831087

clang/test/Frontend/print-stats.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -print-stats \
2+
// RUN: -emit-llvm -x ir /dev/null -o - 2>&1 | FileCheck %s --check-prefix=CHECK-IR
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -print-stats \
4+
// RUN: -emit-llvm -x c /dev/null -o - 2>&1 | FileCheck %s --check-prefix=CHECK-C
5+
6+
// CHECK-IR: *** Source Manager Stats
7+
// CHECK-IR: *** File Manager Stats
8+
// CHECK-IR: *** Virtual File System Stats
9+
10+
// CHECK-C: *** Semantic Analysis Stats
11+
// CHECK-C: *** Analysis Based Warnings Stats
12+
// CHECK-C: *** AST Context Stats
13+
// CHECK-C: *** Decl Stats
14+
// CHECK-C: *** Stmt/Expr Stats
15+
// CHECK-C: *** Preprocessor Stats
16+
// CHECK-C: *** Identifier Table Stats
17+
// CHECK-C: *** HeaderSearch Stats
18+
// CHECK-C: *** Source Manager Stats
19+
// CHECK-C: *** File Manager Stats
20+
// CHECK-C: *** Virtual File System Stats

0 commit comments

Comments
 (0)