Permalink
Browse files

Don't generate prototypes for function that already have them. (Lars …

…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...
1 parent a01657b commit d30bd8366074daa211433af7c32ee33f05fab28d @damellis damellis committed Jul 19, 2012
Showing with 24 additions and 6 deletions.
  1. +24 −6 app/src/processing/app/preproc/PdePreprocessor.java
@@ -316,13 +316,31 @@ private String collapseBraces(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.