Skip to content

Commit dd49533

Browse files
committed
[Clang] fix missing source location for ':' error in macro-expanded case statements
1 parent 9ee4b35 commit dd49533

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ Bug Fixes in This Version
689689
- Fixed type mismatch error when 'builtin-elementwise-math' arguments have different qualifiers, this should be well-formed. (#GH141397)
690690
- Constant evaluation now correctly runs the destructor of a variable declared in
691691
the second clause of a C-style ``for`` loop. (#GH139818)
692+
- Fixed incorrect diagnostic location for missing ``:`` in case statements expanded from macros. (#GH143216)
692693

693694
Bug Fixes to Compiler Builtins
694695
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Basic/DiagnosticCommonKinds.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ let CategoryName = "Parse Issue" in {
8383
def err_expected : Error<"expected %0">;
8484
def err_expected_either : Error<"expected %0 or %1">;
8585
def err_expected_after : Error<"expected %1 after %0">;
86+
def note_macro_expansion : Note<"expanded from macro '%0'">;
8687

8788
def err_param_redefinition : Error<"redefinition of parameter %0">;
8889
def warn_method_param_redefinition : Warning<"redefinition of method parameter %0">;

clang/lib/Parse/ParseStmt.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,9 +833,23 @@ StmtResult Parser::ParseCaseStatement(ParsedStmtContext StmtCtx,
833833
<< FixItHint::CreateReplacement(ColonLoc, ":");
834834
} else {
835835
SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
836+
SourceLocation ExprLoc =
837+
LHS.get() ? LHS.get()->getExprLoc() : SourceLocation();
838+
839+
if (ExpectedLoc.isInvalid() && ExprLoc.isMacroID()) {
840+
ExpectedLoc = PP.getSourceManager().getSpellingLoc(ExprLoc);
841+
}
842+
836843
Diag(ExpectedLoc, diag::err_expected_after)
837844
<< "'case'" << tok::colon
838845
<< FixItHint::CreateInsertion(ExpectedLoc, ":");
846+
847+
if (ExprLoc.isMacroID()) {
848+
Diag(ExprLoc, diag::note_macro_expansion)
849+
<< Lexer::getImmediateMacroNameForDiagnostics(
850+
ExprLoc, PP.getSourceManager(), getLangOpts());
851+
}
852+
839853
ColonLoc = ExpectedLoc;
840854
}
841855

clang/test/Parser/switch-recovery.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,16 @@ void fn1() {
229229
}
230230
} // expected-error{{expected statement}}
231231
}
232+
233+
namespace GH143216 {
234+
#define FOO 1 case 3: // expected-error {{expected ':' after 'case'}}
235+
236+
int f(int x) {
237+
switch (x) {
238+
case FOO // expected-note {{expanded from macro 'FOO'}}
239+
return 0;
240+
default:
241+
return 1;
242+
}
243+
}
244+
}

0 commit comments

Comments
 (0)