From d30bd8366074daa211433af7c32ee33f05fab28d Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Thu, 19 Jul 2012 11:27:56 -0400 Subject: [PATCH] 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 --- .../app/preproc/PdePreprocessor.java | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/app/src/processing/app/preproc/PdePreprocessor.java b/app/src/processing/app/preproc/PdePreprocessor.java index 85ab95ba651..cdf087f4ab2 100644 --- a/app/src/processing/app/preproc/PdePreprocessor.java +++ b/app/src/processing/app/preproc/PdePreprocessor.java @@ -316,13 +316,31 @@ public ArrayList 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 matches = new ArrayList(); - Matcher matcher = pattern.matcher(in); - while (matcher.find()) - matches.add(matcher.group(0) + ";"); + // Find already declared prototypes + ArrayList prototypeMatches = new ArrayList(); + Matcher prototypeMatcher = prototypePattern.matcher(in); + while (prototypeMatcher.find()) + prototypeMatches.add(prototypeMatcher.group(0) + ";"); + + // Find all functions and generate prototypes for them + ArrayList functionMatches = new ArrayList(); + 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; } }