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
This commit is contained in:
David A. Mellis 2012-07-19 11:27:56 -04:00
parent a01657b312
commit d30bd83660
1 changed files with 24 additions and 6 deletions

View File

@ -316,13 +316,31 @@ public class PdePreprocessor {
// XXX: doesn't handle ... varargs // XXX: doesn't handle ... varargs
// XXX: doesn't handle function pointers // 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>(); // Find already declared prototypes
Matcher matcher = pattern.matcher(in); ArrayList<String> prototypeMatches = new ArrayList<String>();
while (matcher.find()) Matcher prototypeMatcher = prototypePattern.matcher(in);
matches.add(matcher.group(0) + ";"); while (prototypeMatcher.find())
prototypeMatches.add(prototypeMatcher.group(0) + ";");
return matches; // 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 functionMatches;
} }
} }