[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 06:56:06 PST 2023


KyleFromKitware created this revision.
KyleFromKitware added a reviewer: clang.
KyleFromKitware created this object with edit policy "Only User: KyleFromKitware (Kyle Edwards)".
KyleFromKitware added a project: clang.
Herald added subscribers: shchenz, kbarton, nemanjai.
Herald added a project: All.
KyleFromKitware requested review of this revision.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

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
@@ -295,6 +295,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,
@@ -609,6 +612,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.491763.patch
Type: text/x-patch
Size: 2942 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230124/c48033e3/attachment-0001.bin>


More information about the cfe-commits mailing list