Skip to content

New preprocessor #2729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Default args values are removed from the prototype. See #2729 (comment)
  • Loading branch information
Federico Fissore committed Mar 25, 2015
commit b1de253e1249a3272de0870c8d954edcf8c24f95
18 changes: 18 additions & 0 deletions app/test/processing/app/preproc/CTagsParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,22 @@ public void shouldDealWithMacros() throws Exception {
assertEquals(18, context.get("firstFunctionAtLine"));
}


@Test
public void shouldRemoveDefaultArgsValues() throws Exception {
String ctagsOutput = "setup\t/tmp/sketch3020016532877959639.cpp\t/^void setup() {$/;\"\tkind:function\tline:2\tsignature:()\treturntype:void\n" +
"loop\t/tmp/sketch3020016532877959639.cpp\t/^void loop() {$/;\"\tkind:function\tline:7\tsignature:()\treturntype:void\n" +
"program\t/tmp/sketch3020016532877959639.cpp\t/^byte program (const byte b1, const byte b2 = 0, const byte b3 = 0, const byte b4 = 0)$/;\"\tkind:function\tline:12\tsignature:(const byte b1, const byte b2 = 0, const byte b3 = 0, const byte b4 = 0)\treturntype:byte\n";

Map<String, Object> context = new HashMap<String, Object>();
context.put("ctagsOutput", ctagsOutput);
new CTagsParser().preprocess(context);
List<String> prototypes = (List<String>) context.get("prototypes");

assertEquals(3, prototypes.size());
assertEquals("void setup();", prototypes.get(0));
assertEquals("void loop();", prototypes.get(1));
assertEquals("byte program(const byte b1, const byte b2, const byte b3, const byte b4);", prototypes.get(2));
}

}
23 changes: 23 additions & 0 deletions arduino-core/src/processing/app/preproc/CTagsParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package processing.app.preproc;

import processing.app.helpers.StringUtils;

import java.util.*;

public class CTagsParser implements PreprocessorChainRing {
Expand Down Expand Up @@ -34,6 +36,7 @@ public void preprocess(Map<String, Object> context) throws Exception {
filterOutUnknownTags(tags);
removeDefinedProtypes(tags);
addPrototypes(tags);
removeDefaultArgsValues(tags);

if (!tags.isEmpty()) {
context.put("firstFunctionAtLine", Integer.valueOf(tags.get(0).get("line")));
Expand All @@ -46,6 +49,26 @@ public void preprocess(Map<String, Object> context) throws Exception {
context.put("prototypes", prototypes);
}

private void removeDefaultArgsValues(List<Map<String, String>> tags) {
for (Map<String, String> tag : tags) {
String prototype = tag.get("prototype");
if (prototype.contains("=")) {
int argsStartAt = prototype.indexOf("(") + 1;
int argsEndAt = prototype.lastIndexOf(")");
String[] args = prototype.substring(argsStartAt, argsEndAt).split(",");
for (int i = 0; i < args.length; i++) {
if (args[i].contains("=")) {
args[i] = args[i].substring(0, args[i].indexOf("="));
}
args[i] = args[i].trim();
}
StringBuilder sb = new StringBuilder(prototype);
sb.replace(argsStartAt, argsEndAt, StringUtils.join(Arrays.asList(args), ", "));
tag.put("prototype", sb.toString());
}
}
}

private List<String> removeEmptyRows(List<String> rows) {
List<String> goodRows = new LinkedList<String>();
for (String row : rows) {
Expand Down