Skip to content

Commit

Permalink
Don't generate prototypes for function that already have them. (Lars …
Browse files Browse the repository at this point in the history
…J. Nielsen)

This searches for prototypes by using the same regular expression used to search for functions definitions, but with "{}" replaced by ";".  Note that it requires the prototype to be formatted identically to the function definition (e.g. matching white-space).

http://code.google.com/p/arduino/issues/detail?id=973
  • Loading branch information
damellis committed Jul 19, 2012
1 parent a01657b commit d30bd83
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions app/src/processing/app/preproc/PdePreprocessor.java
Expand Up @@ -316,13 +316,31 @@ public ArrayList<String> prototypes(String in) {

// XXX: doesn't handle ... varargs
// XXX: doesn't handle function pointers
Pattern pattern = Pattern.compile("[\\w\\[\\]\\*]+\\s+[&\\[\\]\\*\\w\\s]+\\([&,\\[\\]\\*\\w\\s]*\\)(?=\\s*\\{)");
Pattern prototypePattern = Pattern.compile("[\\w\\[\\]\\*]+\\s+[&\\[\\]\\*\\w\\s]+\\([&,\\[\\]\\*\\w\\s]*\\)(?=\\s*;)");
Pattern functionPattern = Pattern.compile("[\\w\\[\\]\\*]+\\s+[&\\[\\]\\*\\w\\s]+\\([&,\\[\\]\\*\\w\\s]*\\)(?=\\s*\\{)");

ArrayList<String> matches = new ArrayList<String>();
Matcher matcher = pattern.matcher(in);
while (matcher.find())
matches.add(matcher.group(0) + ";");
// Find already declared prototypes
ArrayList<String> prototypeMatches = new ArrayList<String>();
Matcher prototypeMatcher = prototypePattern.matcher(in);
while (prototypeMatcher.find())
prototypeMatches.add(prototypeMatcher.group(0) + ";");

// Find all functions and generate prototypes for them
ArrayList<String> functionMatches = new ArrayList<String>();
Matcher functionMatcher = functionPattern.matcher(in);
while (functionMatcher.find())
functionMatches.add(functionMatcher.group(0) + ";");

// Remove generated prototypes that exactly match ones found in the source file
for (int functionIndex=functionMatches.size() - 1; functionIndex >= 0; functionIndex--) {
for (int prototypeIndex=0; prototypeIndex < prototypeMatches.size(); prototypeIndex++) {
if ((functionMatches.get(functionIndex)).equals(prototypeMatches.get(prototypeIndex))) {
functionMatches.remove(functionIndex);
break;
}
}
}

return matches;
return functionMatches;
}
}

0 comments on commit d30bd83

Please sign in to comment.