[clang-tools-extra] 21f5ee0 - [clang-tidy]fix crashing when self include cycles for misc-header-include-cycle (#94636)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 6 16:02:49 PDT 2024


Author: Congcong Cai
Date: 2024-06-07T07:02:45+08:00
New Revision: 21f5ee014db5df4fcf3dfe4350fe9c6627f799ad

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

LOG: [clang-tidy]fix crashing when self include cycles for misc-header-include-cycle (#94636)

Fixes: #94634

Added: 
    clang-tools-extra/test/clang-tidy/checkers/misc/header-include-cycle.self.cpp

Modified: 
    clang-tools-extra/clang-tidy/misc/HeaderIncludeCycleCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/misc/HeaderIncludeCycleCheck.cpp b/clang-tools-extra/clang-tidy/misc/HeaderIncludeCycleCheck.cpp
index fadfdc869d37b..37bc577c646ab 100644
--- a/clang-tools-extra/clang-tidy/misc/HeaderIncludeCycleCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/HeaderIncludeCycleCheck.cpp
@@ -139,8 +139,10 @@ class CyclicDependencyCallbacks : public PPCallbacks {
 
     auto CurrentIt = Files.rbegin();
     do {
-      Check.diag(CurrentIt->Loc, "'%0' included from here", DiagnosticIDs::Note)
-          << CurrentIt->Name;
+      if (CurrentIt->Loc.isValid())
+        Check.diag(CurrentIt->Loc, "'%0' included from here",
+                   DiagnosticIDs::Note)
+            << CurrentIt->Name;
     } while (CurrentIt++ != It);
   }
 

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 661b2b1620d0b..da30aceb8d49d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -317,6 +317,10 @@ Changes in existing checks
   Additionally, the option `UseHeaderFileExtensions` is removed, so that the
   check uses the `HeaderFileExtensions` option unconditionally.
 
+- Improved :doc:`misc-header-include-cycle
+  <clang-tidy/checks/misc/header-include-cycle>` check by avoiding crash for self
+  include cycles.
+
 - Improved :doc:`misc-unused-using-decls
   <clang-tidy/checks/misc/unused-using-decls>` check by replacing the local
   option `HeaderFileExtensions` by the global option of the same name.

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/misc/header-include-cycle.self.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/header-include-cycle.self.cpp
new file mode 100644
index 0000000000000..245dd0a65a8b4
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/header-include-cycle.self.cpp
@@ -0,0 +1,3 @@
+// RUN: not clang-tidy %s -checks='-*,misc-header-include-cycle'
+
+#include "header-include-cycle.self.cpp"


        


More information about the cfe-commits mailing list