Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions clang/lib/Lex/PPExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,9 +903,8 @@ Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro,
SourceLocation ExprStartLoc = SourceMgr.getExpansionLoc(Tok.getLocation());
if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
// Parse error, skip the rest of the macro line.
SourceRange ConditionRange = ExprStartLoc;
if (Tok.isNot(tok::eod))
ConditionRange = DiscardUntilEndOfDirective(Tok);
DiscardUntilEndOfDirective(Tok);

// Restore 'DisableMacroExpansion'.
DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Expand All @@ -916,7 +915,7 @@ Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro,
return {std::nullopt,
false,
DT.IncludedUndefinedIds,
{ExprStartLoc, ConditionRange.getEnd()}};
{ExprStartLoc, Tok.getLocation()}};
}

EvaluatedDefined = DT.State != DefinedTracker::Unknown;
Expand Down Expand Up @@ -948,8 +947,10 @@ Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro,

// Restore 'DisableMacroExpansion'.
DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
SourceRange ValRange = ResVal.getRange();
return {std::nullopt, false, DT.IncludedUndefinedIds, ValRange};
return {std::nullopt,
false,
DT.IncludedUndefinedIds,
{ExprStartLoc, Tok.getLocation()}};
}

if (CheckForEoD) {
Expand Down
21 changes: 19 additions & 2 deletions clang/unittests/Lex/PPCallbacksTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,13 @@ class PPCallbacksTest : public ::testing::Test {
}

std::vector<CondDirectiveCallbacks::Result>
DirectiveExprRange(StringRef SourceText) {
DirectiveExprRange(StringRef SourceText, PreprocessorOptions PPOpts = {}) {
HeaderSearchOptions HSOpts;
TrivialModuleLoader ModLoader;
std::unique_ptr<llvm::MemoryBuffer> Buf =
llvm::MemoryBuffer::getMemBuffer(SourceText);
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get());
PreprocessorOptions PPOpts;
Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader,
/*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false);
PP.Initialize(*Target);
Expand Down Expand Up @@ -569,6 +568,24 @@ TEST_F(PPCallbacksTest, DirectiveExprRanges) {
Lexer::getSourceText(CharSourceRange(Results8[0].ConditionRange, false),
SourceMgr, LangOpts),
"__FILE__ > FLOOFY");

const char *MultiExprIf = "#if defined(FLOOFY) || defined(FLUZZY)\n#endif\n";
const auto &Results9 = DirectiveExprRange(MultiExprIf);
EXPECT_EQ(Results9.size(), 1U);
EXPECT_EQ(
Lexer::getSourceText(CharSourceRange(Results9[0].ConditionRange, false),
SourceMgr, LangOpts),
"defined(FLOOFY) || defined(FLUZZY)");

PreprocessorOptions PPOptsSingleFileParse;
PPOptsSingleFileParse.SingleFileParseMode = true;
const auto &Results10 =
DirectiveExprRange(MultiExprIf, PPOptsSingleFileParse);
EXPECT_EQ(Results10.size(), 1U);
EXPECT_EQ(
Lexer::getSourceText(CharSourceRange(Results10[0].ConditionRange, false),
SourceMgr, LangOpts),
"defined(FLOOFY) || defined(FLUZZY)");
}

} // namespace
Loading