diff options
author | Sven Gothel <[email protected]> | 2023-08-08 11:08:16 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-08-08 11:08:16 +0200 |
commit | e33d67ee14f6a5b999564b40ac0c659db92f2ce0 (patch) | |
tree | 2a314eb52ffc21f885e9bf42562234a9b93bdeea /src/main/java/com/jogamp/gluegen/jcpp/MacroTokenSource.java | |
parent | 00f97cc623469377c59985898b9b765ae66c0aee (diff) | |
parent | 5e50e75ec33f5b4567cabfd60b6baca39524a8b7 (diff) |
Merge remote-tracking branch 'upstream/master' into pulled
# Conflicts:
# build.gradle
# gradle.properties
# gradle/wrapper/gradle-wrapper.jar
# gradle/wrapper/gradle-wrapper.properties
# gradlew
# gradlew.bat
# src/main/java/com/jogamp/gluegen/jcpp/MacroTokenSource.java
# src/main/java/com/jogamp/gluegen/jcpp/Preprocessor.java
# src/main/java/com/jogamp/gluegen/jcpp/SourceIterator.java
# src/main/java/org/anarres/cpp/Main.java
# src/test/java/com/jogamp/gluegen/jcpp/PragmaTest.java
# src/test/java/com/jogamp/gluegen/jcpp/RegressionTest.java
# src/test/java/com/jogamp/gluegen/jcpp/VaArgsPastingTest.java
Diffstat (limited to 'src/main/java/com/jogamp/gluegen/jcpp/MacroTokenSource.java')
-rw-r--r-- | src/main/java/com/jogamp/gluegen/jcpp/MacroTokenSource.java | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/src/main/java/com/jogamp/gluegen/jcpp/MacroTokenSource.java b/src/main/java/com/jogamp/gluegen/jcpp/MacroTokenSource.java index fbb2428..18839c1 100644 --- a/src/main/java/com/jogamp/gluegen/jcpp/MacroTokenSource.java +++ b/src/main/java/com/jogamp/gluegen/jcpp/MacroTokenSource.java @@ -1,6 +1,6 @@ /* * Anarres C Preprocessor - * Copyright (c) 2007-2008, Shevek + * Copyright (c) 2007-2015, Shevek * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import java.io.IOException; import java.util.Iterator; import java.util.List; +import javax.annotation.Nonnegative; import javax.annotation.Nonnull; import static com.jogamp.gluegen.jcpp.Token.*; @@ -28,6 +29,7 @@ import static com.jogamp.gluegen.jcpp.Token.*; * in any inactive context. */ /* pp */ class MacroTokenSource extends Source { + private static final Logger LOG = LoggerFactory.getLogger(MacroTokenSource.class); private final Macro macro; private final Iterator<Token> tokens; /* Pointer into the macro. */ @@ -98,18 +100,34 @@ import static com.jogamp.gluegen.jcpp.Token.*; str.toString(), buf.toString()); } + /** + * Returns true if the given argumentIndex is the last argument of a variadic macro. + * + * @param argumentIndex The index of the argument to inspect. + * @return true if the given argumentIndex is the last argument of a variadic macro. + */ + private boolean isVariadicArgument(@Nonnegative int argumentIndex) { + if (!macro.isVariadic()) + return false; + return argumentIndex == args.size() - 1; + } /* At this point, we have consumed the first M_PASTE. * @see Macro#addPaste(Token) */ private void paste(@Nonnull Token ptok) throws IOException, LexerException { + // List<Token> out = new ArrayList<Token>(); StringBuilder buf = new StringBuilder(); // Token err = null; /* We know here that arg is null or expired, * since we cannot paste an expanded arg. */ int count = 2; + // While I hate auxiliary booleans, this does actually seem to be the simplest solution, + // as it avoids duplicating all the logic around hasNext() in case COMMA. + boolean comma = false; + TOKEN: for (int i = 0; i < count; i++) { if (!tokens.hasNext()) { /* XXX This one really should throw. */ @@ -129,16 +147,29 @@ import static com.jogamp.gluegen.jcpp.Token.*; break; case M_ARG: int idx = ((Integer) tok.getValue()).intValue(); - concat(buf, args.get(idx)); + Argument arg = args.get(idx); + if (comma && isVariadicArgument(idx) && arg.isEmpty()) { + // Ugly way to strip the comma. + buf.setLength(buf.length() - 1); + } else { + concat(buf, arg); + } break; /* XXX Test this. */ case CCOMMENT: case CPPCOMMENT: + // TODO: In cpp, -CC keeps these comments too, + // but turns all C++ comments into C comments. break; + case ',': + comma = true; + buf.append(tok.getText()); + continue TOKEN; default: buf.append(tok.getText()); break; } + comma = false; } /* Push and re-lex. */ |