[PATCH] D134654: [clang] Detect header loops

Nathan Sidwell via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 4 07:30:29 PDT 2022


urnathan updated this revision to Diff 465006.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D134654/new/

https://reviews.llvm.org/D134654

Files:
  clang/include/clang/Basic/FileEntry.h
  clang/lib/Lex/PPDirectives.cpp
  clang/lib/Lex/PPLexerChange.cpp


Index: clang/lib/Lex/PPLexerChange.cpp
===================================================================
--- clang/lib/Lex/PPLexerChange.cpp
+++ clang/lib/Lex/PPLexerChange.cpp
@@ -103,6 +103,10 @@
     }
   }
 
+  if (TheLexer->getPP())
+    if (auto *FE = SourceMgr.getFileEntryForID(FID))
+      FE->EnterOrLeave(true);
+
   EnterSourceFileWithLexer(TheLexer, CurDir);
   return false;
 }
@@ -432,6 +436,10 @@
   // lexing the #includer file.
   if (!IncludeMacroStack.empty()) {
 
+    if (CurPPLexer)
+      if (auto *FE = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))
+        FE->EnterOrLeave(false);
+
     // If we lexed the code-completion file, act as if we reached EOF.
     if (isCodeCompletionEnabled() && CurPPLexer &&
         SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) ==
Index: clang/lib/Lex/PPDirectives.cpp
===================================================================
--- clang/lib/Lex/PPDirectives.cpp
+++ clang/lib/Lex/PPDirectives.cpp
@@ -2329,6 +2329,9 @@
 
   bool IsFirstIncludeOfFile = false;
 
+  if (Action == Enter && File && File->getFileEntry().isEntered())
+    Diag(FilenameTok.getLocation(), diag::warn_include_cycle);
+
   // Ask HeaderInfo if we should enter this #include file.  If not, #including
   // this file will have no effect.
   if (Action == Enter && File &&
Index: clang/include/clang/Basic/FileEntry.h
===================================================================
--- clang/include/clang/Basic/FileEntry.h
+++ clang/include/clang/Basic/FileEntry.h
@@ -359,6 +359,9 @@
   const DirectoryEntry *Dir = nullptr; // Directory file lives in.
   llvm::sys::fs::UniqueID UniqueID;
   unsigned UID = 0; // A unique (small) ID for the file.
+  // Bitfieldiness  vvvvv, mutabley?
+  // Maybe the including machinery should use a map<FileEntry *,unsigned>??
+  mutable unsigned Includedness = 0; // FIXME:DOCUMENT
   bool IsNamedPipe = false;
 
   /// The open file, if it is owned by the \p FileEntry.
@@ -393,6 +396,12 @@
   /// the native FileManager methods).
   bool isNamedPipe() const { return IsNamedPipe; }
 
+  bool isEntered() const { return Includedness != 0; }
+  void EnterOrLeave(bool Enter) const {
+    assert(Enter || Includedness);
+    Includedness += Enter ? +1 : -1;
+  }
+
   void closeFile() const;
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134654.465006.patch
Type: text/x-patch
Size: 2303 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221004/ad56393a/attachment.bin>


More information about the cfe-commits mailing list