Keep track of which source locations are part of a macro argument
instantiation and improve diagnostics which are stem from macro
arguments to trace the argument itself back through the layers of macro
expansion.

This requires some tricky handling of the source locations, as the
argument appears to be expanded in the opposite direction from the
surrounding macro. This patch provides helper routines that encapsulate
the logic and explain the reasoning behind how we step through macros
during diagnostic printing.

This fixes the rest of the test cases originially in PR9279, and later
split out into PR10214 and PR10215.

There is still some more work we can do here to improve the macro
backtrace, but those will follow as separate patches.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@134660 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp
index cd098b2..45922c1 100644
--- a/lib/Basic/SourceManager.cpp
+++ b/lib/Basic/SourceManager.cpp
@@ -531,16 +531,31 @@
   return LastFileIDLookup = FID;
 }
 
-/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
-/// that a token from SpellingLoc should actually be referenced from
-/// InstantiationLoc.
+SourceLocation
+SourceManager::createMacroArgInstantiationLoc(SourceLocation SpellingLoc,
+                                              SourceLocation ILoc,
+                                              unsigned TokLength) {
+  InstantiationInfo II =
+    InstantiationInfo::createForMacroArg(SpellingLoc, ILoc);
+  return createInstantiationLocImpl(II, TokLength);
+}
+
 SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
                                                      SourceLocation ILocStart,
                                                      SourceLocation ILocEnd,
                                                      unsigned TokLength,
                                                      unsigned PreallocatedID,
                                                      unsigned Offset) {
-  InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc);
+  InstantiationInfo II =
+    InstantiationInfo::create(SpellingLoc, ILocStart, ILocEnd);
+  return createInstantiationLocImpl(II, TokLength, PreallocatedID, Offset);
+}
+
+SourceLocation
+SourceManager::createInstantiationLocImpl(const InstantiationInfo &II,
+                                          unsigned TokLength,
+                                          unsigned PreallocatedID,
+                                          unsigned Offset) {
   if (PreallocatedID) {
     // If we're filling in a preallocated ID, just load in the
     // instantiation entry and return.
@@ -824,6 +839,14 @@
   return Res;
 }
 
+bool SourceManager::isMacroArgInstantiation(SourceLocation Loc) const {
+  if (!Loc.isMacroID()) return false;
+
+  FileID FID = getFileID(Loc);
+  const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
+  const SrcMgr::InstantiationInfo &II = E->getInstantiation();
+  return II.isMacroArgInstantiation();
+}
 
 
 //===----------------------------------------------------------------------===//