Skip to content

Commit

Permalink
[Clang] Fix how ReadMacroParameterList handles comments in macro para…
Browse files Browse the repository at this point in the history
…meter-list when in -CC mode

Currently when in -CC mode when processing a function like macro
ReadMacroParameterList(...) does not handle the case where there is a comment
embedded in the parameter-list.

Instead of using LexUnexpandedToken(...) I switched to using
LexUnexpandedNonComment(...) which eats comments while lexing.

Fixes: llvm#60887

Differential Revision: https://reviews.llvm.org/D144511
  • Loading branch information
shafik committed Mar 30, 2023
1 parent a3252d1 commit 20a3fb9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ Bug Fixes in This Version
requires on lambdas when not allowed, which we previously missed.
(`#61748 <https://github.com/llvm/llvm-project/issues/61748>`_)
- Fix confusing diagnostic for incorrect use of qualified concepts names.
- Fix handling of comments in function like macros so they are ignored in -CC
mode.
(`#60887 <https://github.com/llvm/llvm-project/issues/60887>`_)


Bug Fixes to Compiler Builtins
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/Lex/PPDirectives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2643,7 +2643,7 @@ bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
SmallVector<IdentifierInfo*, 32> Parameters;

while (true) {
LexUnexpandedToken(Tok);
LexUnexpandedNonComment(Tok);
switch (Tok.getKind()) {
case tok::r_paren:
// Found the end of the parameter list.
Expand All @@ -2664,7 +2664,7 @@ bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
}

// Lex the token after the identifier.
LexUnexpandedToken(Tok);
LexUnexpandedNonComment(Tok);
if (Tok.isNot(tok::r_paren)) {
Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
return true;
Expand Down Expand Up @@ -2698,7 +2698,7 @@ bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
Parameters.push_back(II);

// Lex the token after the identifier.
LexUnexpandedToken(Tok);
LexUnexpandedNonComment(Tok);

switch (Tok.getKind()) {
default: // #define X(A B
Expand All @@ -2714,7 +2714,7 @@ bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
Diag(Tok, diag::ext_named_variadic_macro);

// Lex the token after the identifier.
LexUnexpandedToken(Tok);
LexUnexpandedNonComment(Tok);
if (Tok.isNot(tok::r_paren)) {
Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
return true;
Expand Down
15 changes: 15 additions & 0 deletions clang/test/Preprocessor/comment_save_macro.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
// RUN: %clang_cc1 -E -C %s | FileCheck -check-prefix=CHECK-C -strict-whitespace %s
// CHECK-C: boo bork bar // zot
// CHECK-C: ( 0 );
// CHECK-C: ( 0,1,2 );
// CHECK-C: ( 0,1,2 );

// RUN: %clang_cc1 -E -CC %s | FileCheck -check-prefix=CHECK-CC -strict-whitespace %s
// CHECK-CC: boo bork /* blah*/ bar // zot
// CHECK-CC: (/**/0/**/);
// CHECK-CC: (/**/0,1,2/**/);
// CHECK-CC: (/**/0,1,2/**/);

// RUN: %clang_cc1 -E %s | FileCheck -strict-whitespace %s
// CHECK: boo bork bar
// CHECK: ( 0 );
// CHECK: ( 0,1,2 );
// CHECK: ( 0,1,2 );


#define FOO bork // blah
boo FOO bar // zot
#define M(/**/x/**/) (/**/x/**/)
M(0);
#define M2(/**/.../**/) (/**/__VA_ARGS__/**/)
M2(0,1,2);
#define M3(/**/x.../**/) (/**/x/**/)
M3(0,1,2);

0 comments on commit 20a3fb9

Please sign in to comment.