Index: base/android/jni_generator/jni_generator.py |
diff --git a/base/android/jni_generator/jni_generator.py b/base/android/jni_generator/jni_generator.py |
index 27335834953a3e006647d27d98b301deb5b1fbf2..f51e5c2ce865b3ad8231ce6dacaf33b982bbe0cb 100755 |
--- a/base/android/jni_generator/jni_generator.py |
+++ b/base/android/jni_generator/jni_generator.py |
@@ -581,7 +581,7 @@ class JNIFromJavaSource(object): |
"""Uses the given java source file to generate the JNI header file.""" |
def __init__(self, contents, fully_qualified_class, options): |
- contents = self._RemoveComments(contents, options) |
+ contents = self._RemoveComments(contents) |
JniParams.SetFullyQualifiedClass(fully_qualified_class) |
JniParams.ExtractImportsAndInnerClasses(contents) |
jni_namespace = ExtractJNINamespace(contents) or options.namespace |
@@ -595,23 +595,28 @@ class JNIFromJavaSource(object): |
[], options) |
self.content = inl_header_file_generator.GetContent() |
- def _RemoveComments(self, contents, options): |
+ # Match single line comments, multiline comments, character literals, and |
+ # double-quoted strings. |
+ _comment_remover_regex = re.compile( |
bulach
2014/04/14 11:30:44
nit: maybe move this before __init__ ? it took me
Torne
2014/04/14 11:41:06
Done.
|
+ r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', |
+ re.DOTALL | re.MULTILINE) |
+ |
+ def _RemoveComments(self, contents): |
bulach
2014/04/14 11:30:44
...and then perhaps make this @staticmethod?
Torne
2014/04/14 11:41:06
Static methods can't access members of the class,
|
# We need to support both inline and block comments, and we need to handle |
- # strings that contain '//' or '/*'. Rather than trying to do all that with |
- # regexps, we just pipe the contents through the C preprocessor. We tell cpp |
- # the file has already been preprocessed, so it just removes comments and |
- # doesn't try to parse #include, #pragma etc. |
- # |
- # TODO(husky): This is a bit hacky. It would be cleaner to use a real Java |
+ # strings that contain '//' or '/*'. |
+ # TODO(bulach): This is a bit hacky. It would be cleaner to use a real Java |
# parser. Maybe we could ditch JNIFromJavaSource and just always use |
# JNIFromJavaP; or maybe we could rewrite this script in Java and use APT. |
# http://code.google.com/p/chromium/issues/detail?id=138941 |
- p = subprocess.Popen(args=[options.cpp, '-fpreprocessed'], |
- stdin=subprocess.PIPE, |
- stdout=subprocess.PIPE, |
- stderr=subprocess.PIPE) |
- stdout, _ = p.communicate(contents) |
- return stdout |
+ def replacer(match): |
+ # Replace matches that are comments with nothing; return literals/strings |
+ # unchanged. |
+ s = match.group(0) |
+ if s.startswith('/'): |
+ return "" |
bulach
2014/04/14 11:30:44
nit: s/""/''/
Torne
2014/04/14 11:41:06
Done.
|
+ else: |
+ return s |
+ return self._comment_remover_regex.sub(replacer, contents) |
def GetContent(self): |
return self.content |