Author: Dmitry Spikhalsky
Date: 2026-10-21T05:51:58Z
New Revision: c0afba08e389d5b6ea4c904725c31c4ae6cd28a5

URL: 
https://github.com/llvm/llvm-project/commit/c0afba08e389d5b6ea4c904725c31c4ae6cd28a5
DIFF: 
https://github.com/llvm/llvm-project/commit/c0afba08e389d5b6ea4c904725c31c4ae6cd28a5.diff

LOG: [clang-tidy] Fix crash on invalid file extension options (#227869)

`ClangTidyContext`'s constructor calls `setCurrentFile("dummy")` before
a `DiagnosticsEngine` is attached. If `HeaderFileExtensions` or
`ImplementationFileExtensions` contains an invalid entry,
`setCurrentFile` emits the "Invalid header file extensions"
configuration diagnostic through the null engine, and clang-tidy crashes
before it analyzes anything.

This keeps parsing the options as before, but emits configuration
diagnostics only once an engine is attached. Every translation unit
calls `setCurrentFile` again after that, so the warning is still
reported for each analyzed file.

Reproduction:

```console
$ echo 'int f();' > main.cpp
$ clang-tidy --checks='-*,modernize-use-trailing-return-type' \
    --config='{HeaderFileExtensions: [".h"]}' main.cpp --
```

Before (clang-tidy 22.1.8): segmentation fault, exit code 139.

After:

```console
warning: Invalid header file extensions [clang-tidy-config]
main.cpp:1:5: warning: use a trailing return type for this function 
[modernize-use-trailing-return-type]
```

Fixes #96806

Assisted-by: Claude Code

Added: 
    clang-tools-extra/test/clang-tidy/infrastructure/invalid-file-extensions.cpp

Modified: 
    clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
    clang-tools-extra/docs/ReleaseNotes.md

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp 
b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
index 61eacd5df6c25..cd163300ec896 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
@@ -245,15 +245,19 @@ void ClangTidyContext::setCurrentFile(StringRef File) {
   WarningAsErrorFilter = std::make_unique<CachedGlobList>(
       StringRef(getOptions().WarningsAsErrors.value_or("")));
   static const std::vector<std::string> EmptyFileExtensions;
+  // The constructor calls this before a DiagnosticsEngine is attached, so only
+  // report configuration problems once one exists, i.e. for analyzed files.
   if (!parseFileExtensions(getOptions().HeaderFileExtensions
                                ? *getOptions().HeaderFileExtensions
                                : EmptyFileExtensions,
-                           HeaderFileExtensions))
+                           HeaderFileExtensions) &&
+      DiagEngine)
     this->configurationDiag("Invalid header file extensions");
   if (!parseFileExtensions(getOptions().ImplementationFileExtensions
                                ? *getOptions().ImplementationFileExtensions
                                : EmptyFileExtensions,
-                           ImplementationFileExtensions))
+                           ImplementationFileExtensions) &&
+      DiagEngine)
     this->configurationDiag("Invalid implementation file extensions");
 }
 

diff  --git a/clang-tools-extra/docs/ReleaseNotes.md 
b/clang-tools-extra/docs/ReleaseNotes.md
index b48662c193442..361d14ec2242c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -139,6 +139,10 @@ infrastructure are described first, followed by 
tool-specific sections.
   piping from {program}`git` to {program}`clang-tidy-
diff .py`, where slashes
   will now be automatically normalized.
 
+- Fixed a crash in {program}`clang-tidy` when `HeaderFileExtensions` or
+  `ImplementationFileExtensions` contains an invalid entry, such as one with a
+  leading dot. The invalid option is now reported as a warning.
+
 #### New checks
 
 - New {doc}`llvm-invalid-regex-pattern

diff  --git 
a/clang-tools-extra/test/clang-tidy/infrastructure/invalid-file-extensions.cpp 
b/clang-tools-extra/test/clang-tidy/infrastructure/invalid-file-extensions.cpp
new file mode 100644
index 0000000000000..61c0aa2276f29
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/infrastructure/invalid-file-extensions.cpp
@@ -0,0 +1,11 @@
+// RUN: clang-tidy -checks='-*,misc-explicit-constructor' 
--config='{HeaderFileExtensions: [".h"], ImplementationFileExtensions: 
[".cpp"]}' %s -- 2>&1 | FileCheck %s
+// RUN: clang-tidy -list-checks -checks='-*,misc-explicit-constructor' 
--config='{HeaderFileExtensions: [".h"]}' 2>&1 | FileCheck 
--check-prefix=CHECK-LIST %s
+
+// CHECK-DAG: warning: Invalid header file extensions [clang-tidy-config]
+// CHECK-DAG: warning: Invalid implementation file extensions 
[clang-tidy-config]
+
+// CHECK-LIST: Enabled checks:
+// CHECK-LIST-NEXT: misc-explicit-constructor
+
+class A { A(int); };
+// CHECK-DAG: :[[@LINE-1]]:11: warning: single-argument constructors must be 
marked explicit


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to