Skip to content

Commit c296b12

Browse files
authored
[clang] Provide to PPCallbacks full expression range even in single file parse mode. (#138358)
Restore the behavior existing prior to fe2eefc. Make reporting of unevaluated directive source range more consistent and with fewer assumptions. In case of a failed evaluation don't assume any specific token and don't assume correct `PPValue` range tracking.
1 parent b32c6d1 commit c296b12

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

clang/lib/Lex/PPExpressions.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -903,9 +903,8 @@ Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro,
903903
SourceLocation ExprStartLoc = SourceMgr.getExpansionLoc(Tok.getLocation());
904904
if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
905905
// Parse error, skip the rest of the macro line.
906-
SourceRange ConditionRange = ExprStartLoc;
907906
if (Tok.isNot(tok::eod))
908-
ConditionRange = DiscardUntilEndOfDirective(Tok);
907+
DiscardUntilEndOfDirective(Tok);
909908

910909
// Restore 'DisableMacroExpansion'.
911910
DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
@@ -916,7 +915,7 @@ Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro,
916915
return {std::nullopt,
917916
false,
918917
DT.IncludedUndefinedIds,
919-
{ExprStartLoc, ConditionRange.getEnd()}};
918+
{ExprStartLoc, Tok.getLocation()}};
920919
}
921920

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

949948
// Restore 'DisableMacroExpansion'.
950949
DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
951-
SourceRange ValRange = ResVal.getRange();
952-
return {std::nullopt, false, DT.IncludedUndefinedIds, ValRange};
950+
return {std::nullopt,
951+
false,
952+
DT.IncludedUndefinedIds,
953+
{ExprStartLoc, Tok.getLocation()}};
953954
}
954955

955956
if (CheckForEoD) {

clang/unittests/Lex/PPCallbacksTest.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,13 @@ class PPCallbacksTest : public ::testing::Test {
237237
}
238238

239239
std::vector<CondDirectiveCallbacks::Result>
240-
DirectiveExprRange(StringRef SourceText) {
240+
DirectiveExprRange(StringRef SourceText, PreprocessorOptions PPOpts = {}) {
241241
HeaderSearchOptions HSOpts;
242242
TrivialModuleLoader ModLoader;
243243
std::unique_ptr<llvm::MemoryBuffer> Buf =
244244
llvm::MemoryBuffer::getMemBuffer(SourceText);
245245
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
246246
HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get());
247-
PreprocessorOptions PPOpts;
248247
Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader,
249248
/*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false);
250249
PP.Initialize(*Target);
@@ -569,6 +568,24 @@ TEST_F(PPCallbacksTest, DirectiveExprRanges) {
569568
Lexer::getSourceText(CharSourceRange(Results8[0].ConditionRange, false),
570569
SourceMgr, LangOpts),
571570
"__FILE__ > FLOOFY");
571+
572+
const char *MultiExprIf = "#if defined(FLOOFY) || defined(FLUZZY)\n#endif\n";
573+
const auto &Results9 = DirectiveExprRange(MultiExprIf);
574+
EXPECT_EQ(Results9.size(), 1U);
575+
EXPECT_EQ(
576+
Lexer::getSourceText(CharSourceRange(Results9[0].ConditionRange, false),
577+
SourceMgr, LangOpts),
578+
"defined(FLOOFY) || defined(FLUZZY)");
579+
580+
PreprocessorOptions PPOptsSingleFileParse;
581+
PPOptsSingleFileParse.SingleFileParseMode = true;
582+
const auto &Results10 =
583+
DirectiveExprRange(MultiExprIf, PPOptsSingleFileParse);
584+
EXPECT_EQ(Results10.size(), 1U);
585+
EXPECT_EQ(
586+
Lexer::getSourceText(CharSourceRange(Results10[0].ConditionRange, false),
587+
SourceMgr, LangOpts),
588+
"defined(FLOOFY) || defined(FLUZZY)");
572589
}
573590

574591
} // namespace

0 commit comments

Comments
 (0)