Skip to content

Commit fad2daa

Browse files
author
Martin Vejnar
committed
Elided __VA_ARGS__ now erase preceding comma in MS mode. bug 12845
1 parent 80fd37a commit fad2daa

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

lib/Lex/TokenLexer.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@ void TokenLexer::ExpandFunctionArguments() {
192192
// input.
193193
MadeChange = true;
194194

195+
if (PP.getLangOpts().MicrosoftMode &&
196+
(unsigned)ArgNo == Macro->getNumArgs()-1 && // is __VA_ARGS__
197+
ActualArgs->isVarargsElidedUse() && // Argument elided.
198+
!ResultToks.empty() && ResultToks.back().is(tok::comma)) {
199+
// Never add a space, even if the comma or arg had a space.
200+
NextTokGetsSpace = false;
201+
202+
// Remove the comma
203+
ResultToks.pop_back();
204+
205+
// If the comma was right after another paste (e.g. "X##,__VA_ARGS__"),
206+
// the paste is ignored by MS compilers.
207+
if (!ResultToks.empty() && ResultToks.back().is(tok::hashhash))
208+
ResultToks.pop_back();
209+
continue;
210+
}
211+
195212
// Otherwise, this is a use of the argument. Find out if there is a paste
196213
// (##) operator before or after the argument.
197214
bool PasteBefore =

test/Preprocessor/macro_fn_comma_swallow.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@
2626
// CHECK: 5: 1
2727
#define X5(x,...) x##,##__VA_ARGS__
2828
5: X5(1)
29+
30+
// should not eat the comma.
31+
// CHECK: 6: {foo,}
32+
#define X6(b, ...) {b,__VA_ARGS__}
33+
6: X6(foo)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test the GNU comma swallowing extension.
2+
// RUN: %clang_cc1 %s -E -fms-compatibility | FileCheck -strict-whitespace %s
3+
4+
// should eat the comma before emtpy varargs
5+
// CHECK: 1: {foo}
6+
#define X1(b, ...) {b,__VA_ARGS__}
7+
1: X1(foo)

0 commit comments

Comments
 (0)