Skip to content

[clang][Basic] Optimize getDiagnosticSeverity() #141950

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
Jun 2, 2025

Conversation

tbaederr
Copy link
Contributor

Try not to call getDiagInfo() as often and only do it if we really have to.

Try not to call getDiagInfo() as often and only do it if we really have
to.
@tbaederr tbaederr requested a review from cor3ntin May 29, 2025 14:00
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 29, 2025
@llvmbot
Copy link
Member

llvmbot commented May 29, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Try not to call getDiagInfo() as often and only do it if we really have to.


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

1 Files Affected:

  • (modified) clang/lib/Basic/DiagnosticIDs.cpp (+21-15)
diff --git a/clang/lib/Basic/DiagnosticIDs.cpp b/clang/lib/Basic/DiagnosticIDs.cpp
index f01ff4df84e6a..ae1484519e812 100644
--- a/clang/lib/Basic/DiagnosticIDs.cpp
+++ b/clang/lib/Basic/DiagnosticIDs.cpp
@@ -543,26 +543,32 @@ DiagnosticIDs::getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc,
     return Result;
 
   const auto &SM = Diag.getSourceManager();
-
-  bool ShowInSystemHeader =
-      IsCustomDiag
-          ? CustomDiagInfo->getDescription(DiagID).ShouldShowInSystemHeader()
-          : !GetDiagInfo(DiagID) || GetDiagInfo(DiagID)->WarnShowInSystemHeader;
-
   // If we are in a system header, we ignore it. We look at the diagnostic class
   // because we also want to ignore extensions and warnings in -Werror and
   // -pedantic-errors modes, which *map* warnings/extensions to errors.
-  if (State->SuppressSystemWarnings && !ShowInSystemHeader && Loc.isValid() &&
-      SM.isInSystemHeader(SM.getExpansionLoc(Loc)))
-    return diag::Severity::Ignored;
-
+  if (State->SuppressSystemWarnings && Loc.isValid() &&
+      SM.isInSystemHeader(SM.getExpansionLoc(Loc))) {
+    bool ShowInSystemHeader = false;
+    if (IsCustomDiag)
+      ShowInSystemHeader =
+          CustomDiagInfo->getDescription(DiagID).ShouldShowInSystemHeader();
+    else if (const StaticDiagInfoRec *Rec = GetDiagInfo(DiagID))
+      ShowInSystemHeader = Rec->WarnShowInSystemHeader;
+
+    if (!ShowInSystemHeader)
+      return diag::Severity::Ignored;
+  }
   // We also ignore warnings due to system macros
-  bool ShowInSystemMacro =
-      !GetDiagInfo(DiagID) || GetDiagInfo(DiagID)->WarnShowInSystemMacro;
-  if (State->SuppressSystemWarnings && !ShowInSystemMacro && Loc.isValid() &&
-      SM.isInSystemMacro(Loc))
-    return diag::Severity::Ignored;
+  if (State->SuppressSystemWarnings && Loc.isValid() &&
+      SM.isInSystemMacro(Loc)) {
+
+    bool ShowInSystemMacro = false;
+    if (const StaticDiagInfoRec *Rec = GetDiagInfo(DiagID))
+      ShowInSystemMacro = Rec->WarnShowInSystemMacro;
 
+    if (!ShowInSystemMacro)
+      return diag::Severity::Ignored;
+  }
   // Clang-diagnostics pragmas always take precedence over suppression mapping.
   if (!Mapping.isPragma() && Diag.isSuppressedViaMapping(DiagID, Loc))
     return diag::Severity::Ignored;

@tbaederr tbaederr merged commit f3f07a3 into llvm:main Jun 2, 2025
11 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jun 2, 2025

LLVM Buildbot has detected a new failure on builder fuchsia-x86_64-linux running on fuchsia-debian-64-us-central1-a-1 while building clang at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/11/builds/16383

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/fuchsia-linux.py ...' (failure)
...
[830/1376] Running the Clang regression tests
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using clang: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/clang
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/subst.py:126: note: Did not find cir-opt in /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin:/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/subst.py:126: note: Did not find clang-repl in /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin:/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/ld.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using lld-link: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/lld-link
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld64.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/ld64.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using wasm-ld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/wasm-ld
-- Testing: 21563 tests, 60 workers --
Testing:  0.. 10.. 20.. 30.. 40.. 
FAIL: Clang :: Driver/cl-inputs.c (11085 of 21563)
******************** TEST 'Clang :: Driver/cl-inputs.c' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/clang --driver-mode=cl /TC -### -- /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/clang/test/Driver/cl-inputs.c 2>&1 | /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/FileCheck -check-prefix=TC /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/clang/test/Driver/cl-inputs.c # RUN: at line 4
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/clang --driver-mode=cl /TC -### -- /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/clang/test/Driver/cl-inputs.c
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/FileCheck -check-prefix=TC /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/clang/test/Driver/cl-inputs.c
/var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/clang/test/Driver/cl-inputs.c:7:12: error: TC-NOT: excluded string found in input
// TC-NOT: note
           ^
<stdin>:6:309: note: found here
 "/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/lld-link" "-out:cl-inputs.exe" "-libpath:lib/amd64" "-libpath:atlmfc/lib/amd64" "-nologo" "/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/lib/clang/21/lib/x86_64-pc-windows-msvc/clang_rt.builtins.lib" "/tmp/lit-tmp-fgixnote/cl-inputs-194e5c.obj"
                                                                                                                                                                                                                                                                                                                    ^~~~

Input file: <stdin>
Check file: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/clang/test/Driver/cl-inputs.c

-dump-input=help explains the following input dump.

Input was:
<<<<<<
       1: Fuchsia clang version 21.0.0git (https://github.com/llvm/llvm-project.git f3f07a3f785d60a3afdc230641b91f044e4b2b99) 
       2: Target: x86_64-pc-windows-msvc 
       3: Thread model: posix 
       4: InstalledDir: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin 
       5:  "/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/llvm" "clang" "-cc1" "-triple" "x86_64-pc-windows-msvc19.33.0" "-emit-obj" "-mincremental-linker-compatible" "-dumpdir" "a-" "-disable-free" "-clear-ast-before-backend" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "cl-inputs.c" "-mrelocation-model" "pic" "-pic-level" "2" "-mframe-pointer=none" "-relaxed-aliasing" "-fmath-errno" "-ffp-contract=on" "-fno-rounding-math" "-mconstructor-aliases" "-fms-volatile" "-funwind-tables=2" "-target-cpu" "x86-64" "-mllvm" "-x86-asm-syntax=intel" "-tune-cpu" "generic" "-D_MT" "-flto-visibility-public-std" "--dependent-lib=libcmt" "--dependent-lib=oldnames" "-stack-protector" "2" "-fdiagnostics-format" "msvc" "-fdebug-compilation-dir=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/tools/clang/test/Driver" "-fcoverage-compilation-dir=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/tools/clang/test/Driver" "-resource-dir" "/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/lib/clang/21" "-internal-isystem" "/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/lib/clang/21/include" "-ferror-limit" "19" "-fno-use-cxa-atexit" "-fms-extensions" "-fms-compatibility" "-fms-compatibility-version=19.33" "-fskip-odr-check-in-gmf" "-fdelayed-template-parsing" "-faddrsig" "-o" "/tmp/lit-tmp-fgixnote/cl-inputs-194e5c.obj" "-x" "c" "/var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/clang/test/Driver/cl-inputs.c" 
       6:  "/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/lld-link" "-out:cl-inputs.exe" "-libpath:lib/amd64" "-libpath:atlmfc/lib/amd64" "-nologo" "/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/lib/clang/21/lib/x86_64-pc-windows-msvc/clang_rt.builtins.lib" "/tmp/lit-tmp-fgixnote/cl-inputs-194e5c.obj" 
not:7                                                                                                                                                                                                                                                                                                                         !~~~                        error: no match expected
>>>>>>

--

********************
Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90.. 
********************
Failed Tests (1):
  Clang :: Driver/cl-inputs.c
Step 7 (check) failure: check (failure)
...
[830/1376] Running the Clang regression tests
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using clang: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/clang
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/subst.py:126: note: Did not find cir-opt in /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin:/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/subst.py:126: note: Did not find clang-repl in /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin:/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/ld.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using lld-link: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/lld-link
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld64.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/ld64.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using wasm-ld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/wasm-ld
-- Testing: 21563 tests, 60 workers --
Testing:  0.. 10.. 20.. 30.. 40.. 
FAIL: Clang :: Driver/cl-inputs.c (11085 of 21563)
******************** TEST 'Clang :: Driver/cl-inputs.c' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/clang --driver-mode=cl /TC -### -- /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/clang/test/Driver/cl-inputs.c 2>&1 | /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/FileCheck -check-prefix=TC /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/clang/test/Driver/cl-inputs.c # RUN: at line 4
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/clang --driver-mode=cl /TC -### -- /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/clang/test/Driver/cl-inputs.c
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/FileCheck -check-prefix=TC /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/clang/test/Driver/cl-inputs.c
/var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/clang/test/Driver/cl-inputs.c:7:12: error: TC-NOT: excluded string found in input
// TC-NOT: note
           ^
<stdin>:6:309: note: found here
 "/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/lld-link" "-out:cl-inputs.exe" "-libpath:lib/amd64" "-libpath:atlmfc/lib/amd64" "-nologo" "/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/lib/clang/21/lib/x86_64-pc-windows-msvc/clang_rt.builtins.lib" "/tmp/lit-tmp-fgixnote/cl-inputs-194e5c.obj"
                                                                                                                                                                                                                                                                                                                    ^~~~

Input file: <stdin>
Check file: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/clang/test/Driver/cl-inputs.c

-dump-input=help explains the following input dump.

Input was:
<<<<<<
       1: Fuchsia clang version 21.0.0git (https://github.com/llvm/llvm-project.git f3f07a3f785d60a3afdc230641b91f044e4b2b99) 
       2: Target: x86_64-pc-windows-msvc 
       3: Thread model: posix 
       4: InstalledDir: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin 
       5:  "/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/llvm" "clang" "-cc1" "-triple" "x86_64-pc-windows-msvc19.33.0" "-emit-obj" "-mincremental-linker-compatible" "-dumpdir" "a-" "-disable-free" "-clear-ast-before-backend" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "cl-inputs.c" "-mrelocation-model" "pic" "-pic-level" "2" "-mframe-pointer=none" "-relaxed-aliasing" "-fmath-errno" "-ffp-contract=on" "-fno-rounding-math" "-mconstructor-aliases" "-fms-volatile" "-funwind-tables=2" "-target-cpu" "x86-64" "-mllvm" "-x86-asm-syntax=intel" "-tune-cpu" "generic" "-D_MT" "-flto-visibility-public-std" "--dependent-lib=libcmt" "--dependent-lib=oldnames" "-stack-protector" "2" "-fdiagnostics-format" "msvc" "-fdebug-compilation-dir=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/tools/clang/test/Driver" "-fcoverage-compilation-dir=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/tools/clang/test/Driver" "-resource-dir" "/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/lib/clang/21" "-internal-isystem" "/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/lib/clang/21/include" "-ferror-limit" "19" "-fno-use-cxa-atexit" "-fms-extensions" "-fms-compatibility" "-fms-compatibility-version=19.33" "-fskip-odr-check-in-gmf" "-fdelayed-template-parsing" "-faddrsig" "-o" "/tmp/lit-tmp-fgixnote/cl-inputs-194e5c.obj" "-x" "c" "/var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/clang/test/Driver/cl-inputs.c" 
       6:  "/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/bin/lld-link" "-out:cl-inputs.exe" "-libpath:lib/amd64" "-libpath:atlmfc/lib/amd64" "-nologo" "/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-f6j7gqyh/lib/clang/21/lib/x86_64-pc-windows-msvc/clang_rt.builtins.lib" "/tmp/lit-tmp-fgixnote/cl-inputs-194e5c.obj" 
not:7                                                                                                                                                                                                                                                                                                                         !~~~                        error: no match expected
>>>>>>

--

********************
Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90.. 
********************
Failed Tests (1):
  Clang :: Driver/cl-inputs.c

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.

4 participants