[PATCH] D142470: [clang][lex] Add PragmaOnce callback to PPCallbacks

Kyle Edwards via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 24 07:08:27 PST 2023


KyleFromKitware updated this revision to Diff 491775.
KyleFromKitware added a comment.

Rebased onto `main` for D141000 <https://reviews.llvm.org/D141000>.


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

https://reviews.llvm.org/D142470

Files:
  clang/include/clang/Lex/PPCallbacks.h
  clang/include/clang/Lex/Preprocessor.h
  clang/lib/Lex/Pragma.cpp


Index: clang/lib/Lex/Pragma.cpp
===================================================================
--- clang/lib/Lex/Pragma.cpp
+++ clang/lib/Lex/Pragma.cpp
@@ -403,7 +403,7 @@
 }
 
 /// HandlePragmaOnce - Handle \#pragma once.  OnceTok is the 'once'.
-void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
+void Preprocessor::HandlePragmaOnce(PragmaIntroducer Introducer, Token &OnceTok) {
   // Don't honor the 'once' when handling the primary source file, unless
   // this is a prefix to a TU, which indicates we're generating a PCH file, or
   // when the main file is a header (e.g. when -xc-header is provided on the
@@ -416,6 +416,9 @@
   // Get the current file lexer we're looking at.  Ignore _Pragma 'files' etc.
   // Mark the file as a once-only file now.
   HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
+
+  if (Callbacks)
+    Callbacks->PragmaOnce(Introducer, OnceTok.getLocation());
 }
 
 void Preprocessor::HandlePragmaMark(PragmaIntroducer Introducer,
@@ -991,7 +994,7 @@
   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
                     Token &OnceTok) override {
     PP.CheckEndOfDirective("pragma once");
-    PP.HandlePragmaOnce(OnceTok);
+    PP.HandlePragmaOnce(Introducer, OnceTok);
   }
 };
 
Index: clang/include/clang/Lex/Preprocessor.h
===================================================================
--- clang/include/clang/Lex/Preprocessor.h
+++ clang/include/clang/Lex/Preprocessor.h
@@ -2615,7 +2615,7 @@
   void HandlePragmaDirective(PragmaIntroducer Introducer);
 
 public:
-  void HandlePragmaOnce(Token &OnceTok);
+  void HandlePragmaOnce(PragmaIntroducer Introducer, Token &OnceTok);
   void HandlePragmaMark(PragmaIntroducer Introducer, Token &MarkTok);
   void HandlePragmaPoison();
   void HandlePragmaSystemHeader(Token &SysHeaderTok);
Index: clang/include/clang/Lex/PPCallbacks.h
===================================================================
--- clang/include/clang/Lex/PPCallbacks.h
+++ clang/include/clang/Lex/PPCallbacks.h
@@ -305,6 +305,9 @@
   virtual void PragmaAssumeNonNullEnd(PragmaIntroducer Introducer,
                                       SourceLocation Loc) {}
 
+  /// Callback invoked when a \#pragma once directive is read.
+  virtual void PragmaOnce(PragmaIntroducer Introducer, SourceLocation Loc) {}
+
   /// Called by Preprocessor::HandleMacroExpandedIdentifier when a
   /// macro invocation is found.
   virtual void MacroExpands(const Token &MacroNameTok,
@@ -627,6 +630,11 @@
     Second->PragmaAssumeNonNullEnd(Introducer, Loc);
   }
 
+  void PragmaOnce(PragmaIntroducer Introducer, SourceLocation Loc) override {
+    First->PragmaOnce(Introducer, Loc);
+    Second->PragmaOnce(Introducer, Loc);
+  }
+
   void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
                     SourceRange Range, const MacroArgs *Args) override {
     First->MacroExpands(MacroNameTok, MD, Range, Args);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142470.491775.patch
Type: text/x-patch
Size: 2942 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230124/963f164d/attachment.bin>


More information about the cfe-commits mailing list