25
25
import com .sonar .cxx .sslr .impl .token .TokenUtils ;
26
26
import java .util .ArrayList ;
27
27
import java .util .List ;
28
- import org .sonar .api .utils .log .Logger ;
29
- import org .sonar .api .utils .log .Loggers ;
30
28
import org .sonar .cxx .parser .CxxKeyword ;
31
29
import org .sonar .cxx .parser .CxxTokenType ;
32
30
36
34
*/
37
35
class PPReplace {
38
36
39
- private static final Logger LOG = Loggers .get (PPReplace .class );
40
37
private final CxxPreprocessor pp ;
41
38
42
39
PPReplace (CxxPreprocessor pp ) {
@@ -95,6 +92,7 @@ int replaceFunctionLikeMacro(PPMacro macro, List<Token> restTokens, List<Token>
95
92
* the replacement-list. The sequence is terminated by the matching ) token, skipping intervening matched pairs of
96
93
* left and right parentheses.
97
94
*/
95
+ @ SuppressWarnings ({"java:S3776" , "java:S1541" })
98
96
private static int extractArguments (List <Token > tokens , List <Token > arguments ) {
99
97
// argument list must start with '('
100
98
int size = tokens .size ();
@@ -149,7 +147,6 @@ private static int extractArguments(List<Token> tokens, List<Token> arguments) {
149
147
}
150
148
}
151
149
152
- LOG .error ("preprocessor 'matchArguments' error, missing ')': {}" , tokens .toString ());
153
150
return 0 ;
154
151
}
155
152
@@ -177,6 +174,7 @@ private List<Token> replaceParams(PPMacro macro, List<Token> arguments) {
177
174
* identifiers (which are not macro-expanded first) and then concatenates the result.
178
175
*
179
176
*/
177
+ @ SuppressWarnings ({"java:S3776" })
180
178
private void handleOperators (List <Token > replacementList , List <String > parameters , List <Token > arguments ,
181
179
List <Token > result ) {
182
180
@@ -196,22 +194,20 @@ private void handleOperators(List<Token> replacementList, List<String> parameter
196
194
//
197
195
// not a token to be replaced by a macro argument
198
196
//
199
- if ((i = handleVaOpt (view , parameters , arguments , result )) <= 0 ) {
200
- if ((i = handleConcatenation (view , parameters , arguments , result )) <= 0 ) {
201
- result .add (token );
202
- }
197
+ if (((i = handleVaOpt (view , parameters , arguments , result )) <= 0 )
198
+ && ((i = handleConcatenation (view , parameters , arguments , result )) <= 0 )) {
199
+ result .add (token );
203
200
}
204
201
} else if (parameterIndex < arguments .size ()) {
205
202
//
206
203
// token to be replaced by a macro argument
207
204
//
208
205
argument = arguments .get (parameterIndex );
209
206
210
- if ((i = handleConcatenation (view , parameters , arguments , result )) <= 0 ) {
211
- if (tokensConsumed < 1 || !handleStringification (
212
- replacementList .subList (tokensConsumed - 1 , replacementList .size ()), argument , result )) {
213
- newValue = expand (argument .getValue ());
214
- }
207
+ if (((i = handleConcatenation (view , parameters , arguments , result )) <= 0 )
208
+ && (tokensConsumed < 1 || !handleStringification (
209
+ replacementList .subList (tokensConsumed - 1 , replacementList .size ()), argument , result ))) {
210
+ newValue = expand (argument .getValue ());
215
211
}
216
212
}
217
213
@@ -259,8 +255,8 @@ private static Token getReplacementToken(Token token, List<String> parameters, L
259
255
* (1) A ## ## B == A ## B
260
256
* (2) A ## B ## C ...
261
257
*/
262
- private int handleConcatenation (List <Token > replacementList , List <String > parameters , List <Token > arguments ,
263
- List <Token > result ) {
258
+ private static int handleConcatenation (List <Token > replacementList , List <String > parameters , List <Token > arguments ,
259
+ List <Token > result ) {
264
260
265
261
int tokensConsumed = 0 ;
266
262
@@ -288,7 +284,7 @@ && isIdentifier(replacementList.get(tokensConsumed).getType())
288
284
* In function-like macros, a # operator before an identifier in the argument-list runs the identifier through
289
285
* parameter argument and encloses the result in quotes, effectively creating a string literal.
290
286
*/
291
- private boolean handleStringification (List <Token > replacementList , Token argument , List <Token > result ) {
287
+ private static boolean handleStringification (List <Token > replacementList , Token argument , List <Token > result ) {
292
288
if (PPPunctuator .HASH .equals (replacementList .get (0 ).getType ())) {
293
289
result .set (result .size () - 1 ,
294
290
PPGeneratedToken .build (argument , argument .getType (),
@@ -307,7 +303,7 @@ private boolean handleStringification(List<Token> replacementList, Token argumen
307
303
* the ## does nothing when the variable arguments are present, but removes the comma when the variable arguments are
308
304
* not present: this makes it possible to define macros such as fprintf (stderr, format, ##__VA_ARGS__).
309
305
*/
310
- private void handleEmptyVaArgs (List <Token > replacementList , List <Token > result ) {
306
+ private static void handleEmptyVaArgs (List <Token > replacementList , List <Token > result ) {
311
307
if (!"__VA_ARGS__" .equals (replacementList .get (0 ).getValue ())) {
312
308
return ;
313
309
}
@@ -329,6 +325,8 @@ private void handleEmptyVaArgs(List<Token> replacementList, List<Token> result)
329
325
case COMMA : // (2)
330
326
result .remove (lastIndex );
331
327
break ;
328
+ default :
329
+ break ;
332
330
}
333
331
}
334
332
}
@@ -344,6 +342,7 @@ private void handleEmptyVaArgs(List<Token> replacementList, List<Token> result)
344
342
* __VA_OPT__ ( pp-tokensopt )
345
343
* </code>
346
344
*/
345
+ @ SuppressWarnings ({"java:S3776" , "java:S1142" })
347
346
private int handleVaOpt (List <Token > replacementList , List <String > parameters , List <Token > arguments ,
348
347
List <Token > result ) {
349
348
var firstIndex = -1 ;
@@ -370,21 +369,21 @@ private int handleVaOpt(List<Token> replacementList, List<String> parameters, Li
370
369
}
371
370
}
372
371
372
+ int consumedTokens = 0 ;
373
+
373
374
if (firstIndex != -1 && lastIndex != -1 ) {
374
375
if (parameters .size () == arguments .size ()) {
375
376
// __VA_OPT__ ( pp-tokensopt ), keep pp-tokensopt
376
377
var ppTokens = replacementList .subList (firstIndex + 1 , lastIndex );
377
378
handleOperators (ppTokens , parameters , arguments , result );
378
- return 2 + ppTokens .size ();
379
+ consumedTokens = 2 + ppTokens .size ();
379
380
} else {
380
381
// remove __VA_OPT__ ( pp-tokensopt )
381
- return 1 + lastIndex - firstIndex ;
382
+ consumedTokens = 1 + lastIndex - firstIndex ;
382
383
}
383
384
}
384
385
385
- LOG .error ("preprocessor '__VA_OPT__* error: {}:{}" ,
386
- replacementList .get (0 ).getLine (), replacementList .get (0 ).getColumn ()); // todo
387
- return 0 ;
386
+ return consumedTokens ;
388
387
}
389
388
390
389
}
0 commit comments