Skip to content

Commit

Permalink
[clang][Sema] Improve diagnostics for function-like macros and update…
Browse files Browse the repository at this point in the history
… related notes
  • Loading branch information
StarOne01 committed Feb 12, 2025
1 parent e7404fb commit e4d2186
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
5 changes: 3 additions & 2 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ Improvements to Clang's diagnostics
which are supposed to only exist once per program, but may get duplicated when
built into a shared library.
- Fixed a bug where Clang's Analysis did not correctly model the destructor behavior of ``union`` members (#GH119415).
- Clang now provides a diagnostic note for ``function-like macros`` that are missing the required parentheses.

- Clang now provides a diagnostic note for function-like macros that are
missing the required parentheses (#GH123038).

Improvements to Clang's time-trace
----------------------------------

Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -10857,8 +10857,8 @@ def err_undeclared_use_suggest : Error<
"use of undeclared %0; did you mean %1?">;
def err_undeclared_var_use_suggest : Error<
"use of undeclared identifier %0; did you mean %1?">;
def err_undeclared_var_use_suggest_func_like_macro
: Error<"use of undeclared identifier %0; did you mean %0(...)?">;
def err_undeclared_var_use_suggest_func_like_macro : Error<
"%0 is defined as an object-like macro; did you mean '%0(...)'?">;
def err_no_template : Error<"no template named %0">;
def err_no_template_suggest : Error<"no template named %0; did you mean %1?">;
def err_no_member_template : Error<"no template named %0 in %1">;
Expand Down
18 changes: 9 additions & 9 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2347,17 +2347,18 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK,
return E;
}

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

if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
if (II->hasMacroDefinition()) {
MacroInfo *MI = SemaRef.PP.getMacroInfo(II);
if (MI && MI->isFunctionLike()) {
SemaRef.Diag(TypoLoc,
diag::err_undeclared_var_use_suggest_func_like_macro)
<< II->getName();
<< II;
SemaRef.Diag(MI->getDefinitionLoc(),
diag::note_function_like_macro_requires_parens)
<< II->getName();
Expand Down Expand Up @@ -2403,11 +2404,10 @@ static void emitEmptyLookupTypoDiagnostic(
if (Ctx)
SemaRef.Diag(TypoLoc, diag::err_no_member) << Typo << Ctx
<< SS.getRange();
else {
if (isFunctionLikeMacro(Typo, SemaRef, TypoLoc))
return;
else if (diagnoseFunctionLikeMacro(SemaRef, Typo, TypoLoc))
return;
else
SemaRef.Diag(TypoLoc, DiagnosticID) << Typo;
}
return;
}

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

if (isFunctionLikeMacro(Name, SemaRef, R.getNameLoc()))
if (diagnoseFunctionLikeMacro(SemaRef, Name, R.getNameLoc()))
return true;

// Emit a special diagnostic for failed member lookups.
Expand Down

0 comments on commit e4d2186

Please sign in to comment.