Skip to content

Commit d30bd83

Browse files
committed
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
1 parent a01657b commit d30bd83

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

app/src/processing/app/preproc/PdePreprocessor.java

+24-6
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,31 @@ public ArrayList<String> prototypes(String in) {
316316

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

321-
ArrayList<String> matches = new ArrayList<String>();
322-
Matcher matcher = pattern.matcher(in);
323-
while (matcher.find())
324-
matches.add(matcher.group(0) + ";");
322+
// Find already declared prototypes
323+
ArrayList<String> prototypeMatches = new ArrayList<String>();
324+
Matcher prototypeMatcher = prototypePattern.matcher(in);
325+
while (prototypeMatcher.find())
326+
prototypeMatches.add(prototypeMatcher.group(0) + ";");
327+
328+
// Find all functions and generate prototypes for them
329+
ArrayList<String> functionMatches = new ArrayList<String>();
330+
Matcher functionMatcher = functionPattern.matcher(in);
331+
while (functionMatcher.find())
332+
functionMatches.add(functionMatcher.group(0) + ";");
333+
334+
// Remove generated prototypes that exactly match ones found in the source file
335+
for (int functionIndex=functionMatches.size() - 1; functionIndex >= 0; functionIndex--) {
336+
for (int prototypeIndex=0; prototypeIndex < prototypeMatches.size(); prototypeIndex++) {
337+
if ((functionMatches.get(functionIndex)).equals(prototypeMatches.get(prototypeIndex))) {
338+
functionMatches.remove(functionIndex);
339+
break;
340+
}
341+
}
342+
}
325343

326-
return matches;
344+
return functionMatches;
327345
}
328346
}

0 commit comments

Comments
 (0)