Skip to content

Commit e4d2186

Browse files
committed
[clang][Sema] Improve diagnostics for function-like macros and update related notes
1 parent e7404fb commit e4d2186

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ Improvements to Clang's diagnostics
128128
which are supposed to only exist once per program, but may get duplicated when
129129
built into a shared library.
130130
- Fixed a bug where Clang's Analysis did not correctly model the destructor behavior of ``union`` members (#GH119415).
131-
- Clang now provides a diagnostic note for ``function-like macros`` that are missing the required parentheses.
132-
131+
- Clang now provides a diagnostic note for function-like macros that are
132+
missing the required parentheses (#GH123038).
133+
133134
Improvements to Clang's time-trace
134135
----------------------------------
135136

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10857,8 +10857,8 @@ def err_undeclared_use_suggest : Error<
1085710857
"use of undeclared %0; did you mean %1?">;
1085810858
def err_undeclared_var_use_suggest : Error<
1085910859
"use of undeclared identifier %0; did you mean %1?">;
10860-
def err_undeclared_var_use_suggest_func_like_macro
10861-
: Error<"use of undeclared identifier %0; did you mean %0(...)?">;
10860+
def err_undeclared_var_use_suggest_func_like_macro : Error<
10861+
"%0 is defined as an object-like macro; did you mean '%0(...)'?">;
1086210862
def err_no_template : Error<"no template named %0">;
1086310863
def err_no_template_suggest : Error<"no template named %0; did you mean %1?">;
1086410864
def err_no_member_template : Error<"no template named %0 in %1">;

clang/lib/Sema/SemaExpr.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,17 +2347,18 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK,
23472347
return E;
23482348
}
23492349

2350-
// Check whether a similar function-like macro exists and suggest it
2351-
static bool isFunctionLikeMacro(const DeclarationName &Name, Sema &SemaRef,
2352-
const SourceLocation &TypoLoc) {
2350+
// Diagnose when a macro cannot be expanded because it's a function-like macro
2351+
// being used as an object-like macro. Returns true if a diagnostic is emitted.
2352+
static bool diagnoseFunctionLikeMacro(Sema &SemaRef, DeclarationName Name,
2353+
SourceLocation TypoLoc) {
23532354

23542355
if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
23552356
if (II->hasMacroDefinition()) {
23562357
MacroInfo *MI = SemaRef.PP.getMacroInfo(II);
23572358
if (MI && MI->isFunctionLike()) {
23582359
SemaRef.Diag(TypoLoc,
23592360
diag::err_undeclared_var_use_suggest_func_like_macro)
2360-
<< II->getName();
2361+
<< II;
23612362
SemaRef.Diag(MI->getDefinitionLoc(),
23622363
diag::note_function_like_macro_requires_parens)
23632364
<< II->getName();
@@ -2403,11 +2404,10 @@ static void emitEmptyLookupTypoDiagnostic(
24032404
if (Ctx)
24042405
SemaRef.Diag(TypoLoc, diag::err_no_member) << Typo << Ctx
24052406
<< SS.getRange();
2406-
else {
2407-
if (isFunctionLikeMacro(Typo, SemaRef, TypoLoc))
2408-
return;
2407+
else if (diagnoseFunctionLikeMacro(SemaRef, Typo, TypoLoc))
2408+
return;
2409+
else
24092410
SemaRef.Diag(TypoLoc, DiagnosticID) << Typo;
2410-
}
24112411
return;
24122412
}
24132413

@@ -2648,7 +2648,7 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R,
26482648
}
26492649
R.clear();
26502650

2651-
if (isFunctionLikeMacro(Name, SemaRef, R.getNameLoc()))
2651+
if (diagnoseFunctionLikeMacro(SemaRef, Name, R.getNameLoc()))
26522652
return true;
26532653

26542654
// Emit a special diagnostic for failed member lookups.

0 commit comments

Comments
 (0)