track "just a little more" location information for macro instantiations.
Now instead of just tracking the expansion history, also track the full
range of the macro that got replaced. For object-like macros, this doesn't
change anything. For _Pragma and function-like macros, this means we track
the locations of the ')'.
This is required for PR3579 because apparently GCC uses the line of the ')'
of a function-like macro as the location to expand __LINE__ to.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64601 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Lex/PPMacroExpansion.cpp b/lib/Lex/PPMacroExpansion.cpp
index 7767543..ae68652 100644
--- a/lib/Lex/PPMacroExpansion.cpp
+++ b/lib/Lex/PPMacroExpansion.cpp
@@ -165,6 +165,10 @@
/// invocation.
MacroArgs *Args = 0;
+ // Remember where the end of the instantiation occurred. For an object-like
+ // macro, this is the identifier. For a function-like macro, this is the ')'.
+ SourceLocation InstantiationEnd = Identifier.getLocation();
+
// If this is a function-like macro, read the arguments.
if (MI->isFunctionLike()) {
// C99 6.10.3p10: If the preprocessing token immediately after the the macro
@@ -177,7 +181,7 @@
// Preprocessor directives used inside macro arguments are not portable, and
// this enables the warning.
InMacroArgs = true;
- Args = ReadFunctionLikeMacroArgs(Identifier, MI);
+ Args = ReadFunctionLikeMacroArgs(Identifier, MI, InstantiationEnd);
// Finished parsing args.
InMacroArgs = false;
@@ -248,7 +252,7 @@
// locations.
SourceLocation Loc =
SourceMgr.createInstantiationLoc(Identifier.getLocation(), InstantiateLoc,
- Identifier.getLength());
+ InstantiationEnd,Identifier.getLength());
Identifier.setLocation(Loc);
// If this is #define X X, we must mark the result as unexpandible.
@@ -263,7 +267,7 @@
}
// Start expanding the macro.
- EnterMacro(Identifier, Args);
+ EnterMacro(Identifier, InstantiationEnd, Args);
// Now that the macro is at the top of the include stack, ask the
// preprocessor to read the next token from it.
@@ -275,7 +279,8 @@
/// invoked to read all of the actual arguments specified for the macro
/// invocation. This returns null on error.
MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
- MacroInfo *MI) {
+ MacroInfo *MI,
+ SourceLocation &MacroEnd) {
// The number of fixed arguments to parse.
unsigned NumFixedArgsLeft = MI->getNumArgs();
bool isVariadic = MI->isVariadic();
@@ -308,8 +313,10 @@
return 0;
} else if (Tok.is(tok::r_paren)) {
// If we found the ) token, the macro arg list is done.
- if (NumParens-- == 0)
+ if (NumParens-- == 0) {
+ MacroEnd = Tok.getLocation();
break;
+ }
} else if (Tok.is(tok::l_paren)) {
++NumParens;
} else if (Tok.is(tok::comma) && NumParens == 0) {
@@ -357,7 +364,7 @@
ArgTokens.push_back(EOFTok);
++NumActuals;
--NumFixedArgsLeft;
- };
+ }
// Okay, we either found the r_paren. Check to see if we parsed too few
// arguments.
@@ -494,6 +501,7 @@
Tok.setKind(tok::string_literal);
Tok.setLength(strlen("\"Mmm dd yyyy\""));
Tok.setLocation(SourceMgr.createInstantiationLoc(DATELoc, Tok.getLocation(),
+ Tok.getLocation(),
Tok.getLength()));
} else if (II == Ident__TIME__) {
if (!TIMELoc.isValid())
@@ -501,6 +509,7 @@
Tok.setKind(tok::string_literal);
Tok.setLength(strlen("\"hh:mm:ss\""));
Tok.setLocation(SourceMgr.createInstantiationLoc(TIMELoc, Tok.getLocation(),
+ Tok.getLocation(),
Tok.getLength()));
} else if (II == Ident__INCLUDE_LEVEL__) {
Diag(Tok, diag::ext_pp_include_level);