From f9cfb2d2dc0837842aec7b7f6134e83403940580 Mon Sep 17 00:00:00 2001
From: Richard Smith <richard-llvm@metafoo.co.uk>
Date: Mon, 4 May 2015 03:15:40 +0000
Subject: [PATCH] Switch PPCallbacks to take the new MacroDefinition instead of
 MacroDirective*, in order to preserve full information on module macro
 expansion.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@236404 91177308-0d34-0410-b5e6-96231b3b80d8
---
 include/clang/Lex/PPCallbacks.h               | 29 ++++++++++---------
 .../clang/Lex/PPConditionalDirectiveRecord.h  |  4 +--
 include/clang/Lex/PreprocessingRecord.h       | 10 +++----
 include/clang/Lex/Preprocessor.h              |  4 +--
 lib/ARCMigrate/ARCMT.cpp                      |  2 +-
 lib/Frontend/PrintPreprocessedOutput.cpp      |  4 +--
 lib/Lex/PPConditionalDirectiveRecord.cpp      |  4 +--
 lib/Lex/PPDirectives.cpp                      | 13 ++++-----
 lib/Lex/PPExpressions.cpp                     |  4 +--
 lib/Lex/PPMacroExpansion.cpp                  | 14 ++++-----
 lib/Lex/PreprocessingRecord.cpp               | 23 +++++++--------
 test/Modules/macros.c                         |  1 +
 tools/libclang/Indexing.cpp                   |  4 +--
 unittests/Basic/SourceManagerTest.cpp         |  2 +-
 14 files changed, 55 insertions(+), 63 deletions(-)

diff --git a/include/clang/Lex/PPCallbacks.h b/include/clang/Lex/PPCallbacks.h
index 056c58aa334..a6b8207e7bc 100644
--- a/include/clang/Lex/PPCallbacks.h
+++ b/include/clang/Lex/PPCallbacks.h
@@ -27,6 +27,7 @@ namespace clang {
   class SourceLocation;
   class Token;
   class IdentifierInfo;
+  class MacroDefinition;
   class MacroDirective;
   class MacroArgs;
 
@@ -238,9 +239,9 @@ public:
 
   /// \brief Called by Preprocessor::HandleMacroExpandedIdentifier when a
   /// macro invocation is found.
-  virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
-                            SourceRange Range, const MacroArgs *Args) {
-  }
+  virtual void MacroExpands(const Token &MacroNameTok,
+                            const MacroDefinition &MD, SourceRange Range,
+                            const MacroArgs *Args) {}
 
   /// \brief Hook called whenever a macro definition is seen.
   virtual void MacroDefined(const Token &MacroNameTok,
@@ -251,12 +252,12 @@ public:
   ///
   /// MD is released immediately following this callback.
   virtual void MacroUndefined(const Token &MacroNameTok,
-                              const MacroDirective *MD) {
+                              const MacroDefinition &MD) {
   }
   
   /// \brief Hook called whenever the 'defined' operator is seen.
   /// \param MD The MacroDirective if the name was a macro, null otherwise.
-  virtual void Defined(const Token &MacroNameTok, const MacroDirective *MD,
+  virtual void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
                        SourceRange Range) {
   }
   
@@ -293,17 +294,17 @@ public:
   /// \brief Hook called whenever an \#ifdef is seen.
   /// \param Loc the source location of the directive.
   /// \param MacroNameTok Information on the token being tested.
-  /// \param MD The MacroDirective if the name was a macro, null otherwise.
+  /// \param MD The MacroDefinition if the name was a macro, null otherwise.
   virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
-                     const MacroDirective *MD) {
+                     const MacroDefinition &MD) {
   }
 
   /// \brief Hook called whenever an \#ifndef is seen.
   /// \param Loc the source location of the directive.
   /// \param MacroNameTok Information on the token being tested.
-  /// \param MD The MacroDirective if the name was a macro, null otherwise.
+  /// \param MD The MacroDefiniton if the name was a macro, null otherwise.
   virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
-                      const MacroDirective *MD) {
+                      const MacroDefinition &MD) {
   }
 
   /// \brief Hook called whenever an \#else is seen.
@@ -434,7 +435,7 @@ public:
     Second->PragmaWarningPop(Loc);
   }
 
-  void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
+  void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
                     SourceRange Range, const MacroArgs *Args) override {
     First->MacroExpands(MacroNameTok, MD, Range, Args);
     Second->MacroExpands(MacroNameTok, MD, Range, Args);
@@ -446,12 +447,12 @@ public:
   }
 
   void MacroUndefined(const Token &MacroNameTok,
-                      const MacroDirective *MD) override {
+                      const MacroDefinition &MD) override {
     First->MacroUndefined(MacroNameTok, MD);
     Second->MacroUndefined(MacroNameTok, MD);
   }
 
-  void Defined(const Token &MacroNameTok, const MacroDirective *MD,
+  void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
                SourceRange Range) override {
     First->Defined(MacroNameTok, MD, Range);
     Second->Defined(MacroNameTok, MD, Range);
@@ -478,14 +479,14 @@ public:
 
   /// \brief Hook called whenever an \#ifdef is seen.
   void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
-             const MacroDirective *MD) override {
+             const MacroDefinition &MD) override {
     First->Ifdef(Loc, MacroNameTok, MD);
     Second->Ifdef(Loc, MacroNameTok, MD);
   }
 
   /// \brief Hook called whenever an \#ifndef is seen.
   void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
-              const MacroDirective *MD) override {
+              const MacroDefinition &MD) override {
     First->Ifndef(Loc, MacroNameTok, MD);
     Second->Ifndef(Loc, MacroNameTok, MD);
   }
diff --git a/include/clang/Lex/PPConditionalDirectiveRecord.h b/include/clang/Lex/PPConditionalDirectiveRecord.h
index 00d2d579735..8c5227561b8 100644
--- a/include/clang/Lex/PPConditionalDirectiveRecord.h
+++ b/include/clang/Lex/PPConditionalDirectiveRecord.h
@@ -91,9 +91,9 @@ private:
   void Elif(SourceLocation Loc, SourceRange ConditionRange,
             ConditionValueKind ConditionValue, SourceLocation IfLoc) override;
   void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
-             const MacroDirective *MD) override;
+             const MacroDefinition &MD) override;
   void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
-              const MacroDirective *MD) override;
+              const MacroDefinition &MD) override;
   void Else(SourceLocation Loc, SourceLocation IfLoc) override;
   void Endif(SourceLocation Loc, SourceLocation IfLoc) override;
 };
diff --git a/include/clang/Lex/PreprocessingRecord.h b/include/clang/Lex/PreprocessingRecord.h
index 5cf74f64380..53367ab854f 100644
--- a/include/clang/Lex/PreprocessingRecord.h
+++ b/include/clang/Lex/PreprocessingRecord.h
@@ -488,10 +488,10 @@ namespace clang {
     }
         
   private:
-    void MacroExpands(const Token &Id, const MacroDirective *MD,
+    void MacroExpands(const Token &Id, const MacroDefinition &MD,
                       SourceRange Range, const MacroArgs *Args) override;
     void MacroDefined(const Token &Id, const MacroDirective *MD) override;
-    void MacroUndefined(const Token &Id, const MacroDirective *MD) override;
+    void MacroUndefined(const Token &Id, const MacroDefinition &MD) override;
     void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
                             StringRef FileName, bool IsAngled,
                             CharSourceRange FilenameRange,
@@ -499,11 +499,11 @@ namespace clang {
                             StringRef RelativePath,
                             const Module *Imported) override;
     void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
-               const MacroDirective *MD) override;
+               const MacroDefinition &MD) override;
     void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
-                const MacroDirective *MD) override;
+                const MacroDefinition &MD) override;
     /// \brief Hook called whenever the 'defined' operator is seen.
-    void Defined(const Token &MacroNameTok, const MacroDirective *MD,
+    void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
                  SourceRange Range) override;
 
     void SourceRangeSkipped(SourceRange Range) override;
diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h
index 07e1beb677d..3f668e1e277 100644
--- a/include/clang/Lex/Preprocessor.h
+++ b/include/clang/Lex/Preprocessor.h
@@ -357,9 +357,9 @@ class Preprocessor : public RefCountedBase<Preprocessor> {
 
   struct MacroExpandsInfo {
     Token Tok;
-    MacroDirective *MD;
+    MacroDefinition MD;
     SourceRange Range;
-    MacroExpandsInfo(Token Tok, MacroDirective *MD, SourceRange Range)
+    MacroExpandsInfo(Token Tok, MacroDefinition MD, SourceRange Range)
       : Tok(Tok), MD(MD), Range(Range) { }
   };
   SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks;
diff --git a/lib/ARCMigrate/ARCMT.cpp b/lib/ARCMigrate/ARCMT.cpp
index 0a61cfea6c4..f266eaf8396 100644
--- a/lib/ARCMigrate/ARCMT.cpp
+++ b/lib/ARCMigrate/ARCMT.cpp
@@ -432,7 +432,7 @@ public:
   ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
     : ARCMTMacroLocs(ARCMTMacroLocs) { }
 
-  void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
+  void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
                     SourceRange Range, const MacroArgs *Args) override {
     if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
       ARCMTMacroLocs.push_back(MacroNameTok.getLocation());
diff --git a/lib/Frontend/PrintPreprocessedOutput.cpp b/lib/Frontend/PrintPreprocessedOutput.cpp
index b5e5d0aa037..8ca36489ee4 100644
--- a/lib/Frontend/PrintPreprocessedOutput.cpp
+++ b/lib/Frontend/PrintPreprocessedOutput.cpp
@@ -170,7 +170,7 @@ public:
 
   /// MacroUndefined - This hook is called whenever a macro #undef is seen.
   void MacroUndefined(const Token &MacroNameTok,
-                      const MacroDirective *MD) override;
+                      const MacroDefinition &MD) override;
 };
 }  // end anonymous namespace
 
@@ -361,7 +361,7 @@ void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
 }
 
 void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok,
-                                              const MacroDirective *MD) {
+                                              const MacroDefinition &MD) {
   // Only print out macro definitions in -dD mode.
   if (!DumpDefines) return;
 
diff --git a/lib/Lex/PPConditionalDirectiveRecord.cpp b/lib/Lex/PPConditionalDirectiveRecord.cpp
index 99b87a0a152..12a77849b8b 100644
--- a/lib/Lex/PPConditionalDirectiveRecord.cpp
+++ b/lib/Lex/PPConditionalDirectiveRecord.cpp
@@ -84,14 +84,14 @@ void PPConditionalDirectiveRecord::If(SourceLocation Loc,
 
 void PPConditionalDirectiveRecord::Ifdef(SourceLocation Loc,
                                          const Token &MacroNameTok,
-                                         const MacroDirective *MD) {
+                                         const MacroDefinition &MD) {
   addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
   CondDirectiveStack.push_back(Loc);
 }
 
 void PPConditionalDirectiveRecord::Ifndef(SourceLocation Loc,
                                           const Token &MacroNameTok,
-                                          const MacroDirective *MD) {
+                                          const MacroDefinition &MD) {
   addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
   CondDirectiveStack.push_back(Loc);
 }
diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp
index e1a67f9e9d1..d1c89062761 100644
--- a/lib/Lex/PPDirectives.cpp
+++ b/lib/Lex/PPDirectives.cpp
@@ -2293,17 +2293,15 @@ void Preprocessor::HandleUndefDirective(Token &UndefTok) {
 
   // Okay, we have a valid identifier to undef.
   auto *II = MacroNameTok.getIdentifierInfo();
+  auto MD = getMacroDefinition(II);
 
   // If the callbacks want to know, tell them about the macro #undef.
   // Note: no matter if the macro was defined or not.
-  if (Callbacks) {
-    // FIXME: Tell callbacks about module macros.
-    MacroDirective *MD = getLocalMacroDirective(II);
+  if (Callbacks)
     Callbacks->MacroUndefined(MacroNameTok, MD);
-  }
 
   // If the macro is not defined, this is a noop undef, just return.
-  const MacroInfo *MI = getMacroInfo(II);
+  const MacroInfo *MI = MD.getMacroInfo();
   if (!MI)
     return;
 
@@ -2348,7 +2346,8 @@ void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
   CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
 
   IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
-  MacroInfo *MI = getMacroInfo(MII);
+  auto MD = getMacroDefinition(MII);
+  MacroInfo *MI = MD.getMacroInfo();
 
   if (CurPPLexer->getConditionalStackDepth() == 0) {
     // If the start of a top-level #ifdef and if the macro is not defined,
@@ -2367,8 +2366,6 @@ void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
     markMacroAsUsed(MI);
 
   if (Callbacks) {
-    // FIXME: Tell callbacks about module macros.
-    MacroDirective *MD = getLocalMacroDirective(MII);
     if (isIfndef)
       Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
     else
diff --git a/lib/Lex/PPExpressions.cpp b/lib/Lex/PPExpressions.cpp
index 411a5012241..44513023395 100644
--- a/lib/Lex/PPExpressions.cpp
+++ b/lib/Lex/PPExpressions.cpp
@@ -142,9 +142,7 @@ static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
 
   // Invoke the 'defined' callback.
   if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
-    // FIXME: Tell callbacks about module macros.
-    MacroDirective *MD = Macro.getLocalDirective();
-    Callbacks->Defined(macroToken, MD,
+    Callbacks->Defined(macroToken, Macro,
                        SourceRange(beginLoc, PeekTok.getLocation()));
   }
 
diff --git a/lib/Lex/PPMacroExpansion.cpp b/lib/Lex/PPMacroExpansion.cpp
index edeed42c17e..5af0205af37 100644
--- a/lib/Lex/PPMacroExpansion.cpp
+++ b/lib/Lex/PPMacroExpansion.cpp
@@ -419,10 +419,9 @@ bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
 
   // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
   if (MI->isBuiltinMacro()) {
-    // FIXME: Tell callbacks about module macros.
-    if (Callbacks) Callbacks->MacroExpands(Identifier, M.getLocalDirective(),
-                                           Identifier.getLocation(),
-                                           /*Args=*/nullptr);
+    if (Callbacks)
+      Callbacks->MacroExpands(Identifier, M, Identifier.getLocation(),
+                              /*Args=*/nullptr);
     ExpandBuiltinMacro(Identifier);
     return true;
   }
@@ -468,13 +467,10 @@ bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
       // reading the function macro arguments. To ensure, in that case, that
       // MacroExpands callbacks still happen in source order, queue this
       // callback to have it happen after the function macro callback.
-      // FIXME: Tell callbacks about module macros.
       DelayedMacroExpandsCallbacks.push_back(
-          MacroExpandsInfo(Identifier, M.getLocalDirective(), ExpansionRange));
+          MacroExpandsInfo(Identifier, M, ExpansionRange));
     } else {
-      // FIXME: Tell callbacks about module macros.
-      Callbacks->MacroExpands(Identifier, M.getLocalDirective(), ExpansionRange,
-                              Args);
+      Callbacks->MacroExpands(Identifier, M, ExpansionRange, Args);
       if (!DelayedMacroExpandsCallbacks.empty()) {
         for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) {
           MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i];
diff --git a/lib/Lex/PreprocessingRecord.cpp b/lib/Lex/PreprocessingRecord.cpp
index fd4bed17797..a423041a2d9 100644
--- a/lib/Lex/PreprocessingRecord.cpp
+++ b/lib/Lex/PreprocessingRecord.cpp
@@ -381,27 +381,27 @@ void PreprocessingRecord::addMacroExpansion(const Token &Id,
 }
 
 void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
-                                const MacroDirective *MD) {
+                                const MacroDefinition &MD) {
   // This is not actually a macro expansion but record it as a macro reference.
   if (MD)
-    addMacroExpansion(MacroNameTok, MD->getMacroInfo(),
+    addMacroExpansion(MacroNameTok, MD.getMacroInfo(),
                       MacroNameTok.getLocation());
 }
 
 void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
-                                 const MacroDirective *MD) {
+                                 const MacroDefinition &MD) {
   // This is not actually a macro expansion but record it as a macro reference.
   if (MD)
-    addMacroExpansion(MacroNameTok, MD->getMacroInfo(),
+    addMacroExpansion(MacroNameTok, MD.getMacroInfo(),
                       MacroNameTok.getLocation());
 }
 
 void PreprocessingRecord::Defined(const Token &MacroNameTok,
-                                  const MacroDirective *MD,
+                                  const MacroDefinition &MD,
                                   SourceRange Range) {
   // This is not actually a macro expansion but record it as a macro reference.
   if (MD)
-    addMacroExpansion(MacroNameTok, MD->getMacroInfo(),
+    addMacroExpansion(MacroNameTok, MD.getMacroInfo(),
                       MacroNameTok.getLocation());
 }
 
@@ -409,10 +409,11 @@ void PreprocessingRecord::SourceRangeSkipped(SourceRange Range) {
   SkippedRanges.push_back(Range);
 }
 
-void PreprocessingRecord::MacroExpands(const Token &Id,const MacroDirective *MD,
+void PreprocessingRecord::MacroExpands(const Token &Id,
+                                       const MacroDefinition &MD,
                                        SourceRange Range,
                                        const MacroArgs *Args) {
-  addMacroExpansion(Id, MD->getMacroInfo(), Range);
+  addMacroExpansion(Id, MD.getMacroInfo(), Range);
 }
 
 void PreprocessingRecord::MacroDefined(const Token &Id,
@@ -426,10 +427,8 @@ void PreprocessingRecord::MacroDefined(const Token &Id,
 }
 
 void PreprocessingRecord::MacroUndefined(const Token &Id,
-                                         const MacroDirective *MD) {
-  // Note: MI may be null (when #undef'ining an undefined macro).
-  if (MD)
-    MacroDefinitions.erase(MD->getMacroInfo());
+                                         const MacroDefinition &MD) {
+  MD.forAllDefinitions([&](MacroInfo *MI) { MacroDefinitions.erase(MI); });
 }
 
 void PreprocessingRecord::InclusionDirective(
diff --git a/test/Modules/macros.c b/test/Modules/macros.c
index 076166966b7..7b7b52aa017 100644
--- a/test/Modules/macros.c
+++ b/test/Modules/macros.c
@@ -1,5 +1,6 @@
 // RUN: rm -rf %t
 // RUN: %clang_cc1 -fmodules -x objective-c -verify -fmodules-cache-path=%t -I %S/Inputs %s
+// RUN: %clang_cc1 -fmodules -x objective-c -verify -fmodules-cache-path=%t -I %S/Inputs %s -detailed-preprocessing-record
 // RUN: %clang_cc1 -fmodules -DLOCAL_VISIBILITY -fmodules-local-submodule-visibility -x objective-c -verify -fmodules-cache-path=%t -I %S/Inputs %s
 // RUN: not %clang_cc1 -E -fmodules -x objective-c -fmodules-cache-path=%t -I %S/Inputs %s | FileCheck -check-prefix CHECK-PREPROCESSED %s
 // FIXME: When we have a syntax for modules in C, use that.
diff --git a/tools/libclang/Indexing.cpp b/tools/libclang/Indexing.cpp
index 20df33e8420..0ede684a18e 100644
--- a/tools/libclang/Indexing.cpp
+++ b/tools/libclang/Indexing.cpp
@@ -284,10 +284,10 @@ public:
   /// MacroUndefined - This hook is called whenever a macro #undef is seen.
   /// MI is released immediately following this callback.
   void MacroUndefined(const Token &MacroNameTok,
-                      const MacroDirective *MD) override {}
+                      const MacroDefinition &MD) override {}
 
   /// MacroExpands - This is called by when a macro invocation is found.
-  void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
+  void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
                     SourceRange Range, const MacroArgs *Args) override {}
 
   /// SourceRangeSkipped - This hook is called when a source range is skipped.
diff --git a/unittests/Basic/SourceManagerTest.cpp b/unittests/Basic/SourceManagerTest.cpp
index a151bd5a1f6..494c27a2f1c 100644
--- a/unittests/Basic/SourceManagerTest.cpp
+++ b/unittests/Basic/SourceManagerTest.cpp
@@ -263,7 +263,7 @@ public:
                                  MacroNameTok.getIdentifierInfo()->getName(),
                                  true));
   }
-  void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
+  void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
                     SourceRange Range, const MacroArgs *Args) override {
     Macros.push_back(MacroAction(MacroNameTok.getLocation(),
                                  MacroNameTok.getIdentifierInfo()->getName(),
-- 
GitLab