[preprocessor] Enhance the preprocessor callbacks:

-Add location parameter for the directives callbacks
-Skip callbacks if the directive is inside a skipped range.
-Make sure the directive callbacks are invoked in source order.

llvm-svn: 152017
This commit is contained in:
Argyrios Kyrtzidis 2012-03-05 05:48:09 +00:00
parent 4b02a29eba
commit c793a61355
2 changed files with 55 additions and 51 deletions

View File

@ -192,33 +192,34 @@ public:
/// If -- This hook is called whenever an #if is seen.
/// \param Range The SourceRange of the expression being tested.
// FIXME: better to pass in a list (or tree!) of Tokens.
virtual void If(SourceRange Range) {
virtual void If(SourceLocation Loc, SourceRange ConditionRange) {
}
/// Elif -- This hook is called whenever an #elif is seen.
/// \param Range The SourceRange of the expression being tested.
// FIXME: better to pass in a list (or tree!) of Tokens.
virtual void Elif(SourceRange Range) {
virtual void Elif(SourceLocation Loc, SourceRange ConditionRange,
SourceLocation IfLoc) {
}
/// Ifdef -- This hook is called whenever an #ifdef is seen.
/// \param Loc The location of the token being tested.
/// \param II Information on the token being tested.
virtual void Ifdef(const Token &MacroNameTok) {
virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok) {
}
/// Ifndef -- This hook is called whenever an #ifndef is seen.
/// \param Loc The location of the token being tested.
/// \param II Information on the token being tested.
virtual void Ifndef(const Token &MacroNameTok) {
virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok) {
}
/// Else -- This hook is called whenever an #else is seen.
virtual void Else() {
virtual void Else(SourceLocation Loc, SourceLocation IfLoc) {
}
/// Endif -- This hook is called whenever an #endif is seen.
virtual void Endif() {
virtual void Endif(SourceLocation Loc, SourceLocation IfLoc) {
}
};
@ -335,39 +336,40 @@ public:
}
/// If -- This hook is called whenever an #if is seen.
virtual void If(SourceRange Range) {
First->If(Range);
Second->If(Range);
virtual void If(SourceLocation Loc, SourceRange ConditionRange) {
First->If(Loc, ConditionRange);
Second->If(Loc, ConditionRange);
}
/// Elif -- This hook is called whenever an #if is seen.
virtual void Elif(SourceRange Range) {
First->Elif(Range);
Second->Elif(Range);
virtual void Elif(SourceLocation Loc, SourceRange ConditionRange,
SourceLocation IfLoc) {
First->Elif(Loc, ConditionRange, IfLoc);
Second->Elif(Loc, ConditionRange, IfLoc);
}
/// Ifdef -- This hook is called whenever an #ifdef is seen.
virtual void Ifdef(const Token &MacroNameTok) {
First->Ifdef(MacroNameTok);
Second->Ifdef(MacroNameTok);
virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok) {
First->Ifdef(Loc, MacroNameTok);
Second->Ifdef(Loc, MacroNameTok);
}
/// Ifndef -- This hook is called whenever an #ifndef is seen.
virtual void Ifndef(const Token &MacroNameTok) {
First->Ifndef(MacroNameTok);
Second->Ifndef(MacroNameTok);
virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok) {
First->Ifndef(Loc, MacroNameTok);
Second->Ifndef(Loc, MacroNameTok);
}
/// Else -- This hook is called whenever an #else is seen.
virtual void Else() {
First->Else();
Second->Else();
virtual void Else(SourceLocation Loc, SourceLocation IfLoc) {
First->Else(Loc, IfLoc);
Second->Else(Loc, IfLoc);
}
/// Endif -- This hook is called whenever an #endif is seen.
virtual void Endif() {
First->Endif();
Second->Endif();
virtual void Endif(SourceLocation Loc, SourceLocation IfLoc) {
First->Endif(Loc, IfLoc);
Second->Endif(Loc, IfLoc);
}
};

View File

@ -313,9 +313,6 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
/*foundnonskip*/false,
/*foundelse*/false);
if (Callbacks)
Callbacks->Endif();
}
} else if (Directive[0] == 'e') {
StringRef Sub = Directive.substr(1);
@ -328,8 +325,11 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
assert(!InCond && "Can't be skipping if not in a conditional!");
// If we popped the outermost skipping block, we're done skipping!
if (!CondInfo.WasSkipping)
if (!CondInfo.WasSkipping) {
if (Callbacks)
Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
break;
}
} else if (Sub == "lse") { // "else".
// #else directive in a skipping conditional. If not in some other
// skipping conditional, and if #else hasn't already been seen, enter it
@ -342,14 +342,13 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
// Note that we've seen a #else in this conditional.
CondInfo.FoundElse = true;
if (Callbacks)
Callbacks->Else();
// If the conditional is at the top level, and the #if block wasn't
// entered, enter the #else block now.
if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
CondInfo.FoundNonSkip = true;
CheckEndOfDirective("else");
if (Callbacks)
Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
break;
} else {
DiscardUntilEndOfDirective(); // C99 6.10p4.
@ -378,12 +377,13 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
// If this is a #elif with a #else before it, report the error.
if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
if (Callbacks)
Callbacks->Elif(SourceRange(ConditionalBegin, ConditionalEnd));
// If this condition is true, enter it!
if (ShouldEnter) {
CondInfo.FoundNonSkip = true;
if (Callbacks)
Callbacks->Elif(Tok.getLocation(),
SourceRange(ConditionalBegin, ConditionalEnd),
CondInfo.IfLoc);
break;
}
}
@ -1897,6 +1897,13 @@ void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
if (MI) // Mark it used.
markMacroAsUsed(MI);
if (Callbacks) {
if (isIfndef)
Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok);
else
Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok);
}
// Should we include the stuff contained by this directive?
if (!MI == isIfndef) {
// Yes, remember that we are inside a conditional, then lex the next token.
@ -1909,13 +1916,6 @@ void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
/*Foundnonskip*/false,
/*FoundElse*/false);
}
if (Callbacks) {
if (isIfndef)
Callbacks->Ifndef(MacroNameTok);
else
Callbacks->Ifdef(MacroNameTok);
}
}
/// HandleIfDirective - Implements the #if directive.
@ -1939,6 +1939,10 @@ void Preprocessor::HandleIfDirective(Token &IfToken,
CurPPLexer->MIOpt.EnterTopLevelConditional();
}
if (Callbacks)
Callbacks->If(IfToken.getLocation(),
SourceRange(ConditionalBegin, ConditionalEnd));
// Should we include the stuff contained by this directive?
if (ConditionalTrue) {
// Yes, remember that we are inside a conditional, then lex the next token.
@ -1949,9 +1953,6 @@ void Preprocessor::HandleIfDirective(Token &IfToken,
SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
/*FoundElse*/false);
}
if (Callbacks)
Callbacks->If(SourceRange(ConditionalBegin, ConditionalEnd));
}
/// HandleEndifDirective - Implements the #endif directive.
@ -1977,7 +1978,7 @@ void Preprocessor::HandleEndifDirective(Token &EndifToken) {
"This code should only be reachable in the non-skipping case!");
if (Callbacks)
Callbacks->Endif();
Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
}
/// HandleElseDirective - Implements the #else directive.
@ -2001,12 +2002,12 @@ void Preprocessor::HandleElseDirective(Token &Result) {
// If this is a #else with a #else before it, report the error.
if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
if (Callbacks)
Callbacks->Else(Result.getLocation(), CI.IfLoc);
// Finally, skip the rest of the contents of this block.
SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
/*FoundElse*/true, Result.getLocation());
if (Callbacks)
Callbacks->Else();
}
/// HandleElifDirective - Implements the #elif directive.
@ -2034,11 +2035,12 @@ void Preprocessor::HandleElifDirective(Token &ElifToken) {
// If this is a #elif with a #else before it, report the error.
if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
if (Callbacks)
Callbacks->Elif(ElifToken.getLocation(),
SourceRange(ConditionalBegin, ConditionalEnd), CI.IfLoc);
// Finally, skip the rest of the contents of this block.
SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
/*FoundElse*/CI.FoundElse,
ElifToken.getLocation());
if (Callbacks)
Callbacks->Elif(SourceRange(ConditionalBegin, ConditionalEnd));
}