Skip to content

[Clang] fix missing source location for errors in macro-expanded #143460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 11, 2025
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
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ Bug Fixes in This Version
- Constant evaluation now correctly runs the destructor of a variable declared in
the second clause of a C-style ``for`` loop. (#GH139818)
- Fixed a bug with constexpr evaluation for structs containing unions in case of C++ modules. (#GH143168)
- Fixed incorrect token location when emitting diagnostics for tokens expanded from macros. (#GH143216)

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
4 changes: 1 addition & 3 deletions clang/include/clang/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,7 @@ class Parser : public CodeCompletionHandler {
return ConsumeToken();
}

SourceLocation getEndOfPreviousToken() {
return PP.getLocForEndOfToken(PrevTokLocation);
}
SourceLocation getEndOfPreviousToken() const;

/// GetLookAheadToken - This peeks ahead N tokens and returns that token
/// without consuming any tokens. LookAhead(0) returns 'Tok', LookAhead(1)
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Parse/ParseExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,8 @@ bool Parser::ParseOptionalCXXScopeSpecifier(
// like we never saw it.
Token Identifier = Tok; // Stash away the identifier.
ConsumeToken(); // Eat the identifier, current token is now '::'.
Diag(PP.getLocForEndOfToken(ConsumeToken()), diag::err_expected)
<< tok::identifier;
ConsumeToken();
Diag(getEndOfPreviousToken(), diag::err_expected) << tok::identifier;
UnconsumeToken(Identifier); // Stick the identifier back.
Next = NextToken(); // Point Next at the '{' token.
}
Expand Down
7 changes: 5 additions & 2 deletions clang/lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,10 +832,13 @@ StmtResult Parser::ParseCaseStatement(ParsedStmtContext StmtCtx,
<< "'case'" << tok::colon
<< FixItHint::CreateReplacement(ColonLoc, ":");
} else {
SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
SourceLocation ExpectedLoc = getEndOfPreviousToken();

Diag(ExpectedLoc, diag::err_expected_after)
<< "'case'" << tok::colon
<< FixItHint::CreateInsertion(ExpectedLoc, ":");
<< FixItHint::CreateInsertion(ExpectedLoc,
tok::getTokenName(tok::colon));

ColonLoc = ExpectedLoc;
}

Expand Down
5 changes: 5 additions & 0 deletions clang/lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,11 @@ Parser::TryAnnotateName(CorrectionCandidateCallback *CCC,
return AnnotatedNameKind::Unresolved;
}

SourceLocation Parser::getEndOfPreviousToken() const {
SourceLocation TokenEndLoc = PP.getLocForEndOfToken(PrevTokLocation);
return TokenEndLoc.isValid() ? TokenEndLoc : Tok.getLocation();
}

bool Parser::TryKeywordIdentFallback(bool DisableKeyword) {
assert(Tok.isNot(tok::identifier));
Diag(Tok, diag::ext_keyword_as_ident)
Expand Down
22 changes: 22 additions & 0 deletions clang/test/Parser/macro-expansion-recovery.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s

namespace GH143216 {
#define A x y
enum { A }; // expected-error {{missing ',' between enumerators}}

#define B x y
void f() {
int a[2];
auto [B] = a; // expected-error {{expected ','}}
}

#define C <int!
template <class T> class D;
D C; // expected-error {{expected unqualified-id}} \
// expected-error {{expected '>'}} \
// expected-note {{to match this '<'}}

#define E F::{
class F { E }}; // expected-error {{expected identifier}} \
// expected-error {{expected member name or ';' after declaration specifiers}}
}
13 changes: 13 additions & 0 deletions clang/test/Parser/switch-recovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,16 @@ void fn1() {
}
} // expected-error{{expected statement}}
}

namespace GH143216 {
#define FOO 1 case 3:

int f(int x) {
switch (x) {
case FOO // expected-error {{expected ':' after 'case'}}
return 0;
default:
return 1;
}
}
}