Use the correct comment format for the Kythe metadata.
Previously I saw that the metadata reader supported C++-style comments,
and also supported line wrapping, so I figured it supported both. Turns
out it doesn't. For C++-style comments it just takes the first line.
The new output matches that of the Kythe verifier test for this feature:
https://github.com/kythe/kythe/blob/master/kythe/cxx/indexer/cxx/testdata/metadata/metadata_inline_cstyle.cc
We make sure to support both the new and old format when stripping
existing metadata from the file. This has been tested locally on files
containing both styles.
Bug: 615202
Change-Id: I5497c770f051d9f69feeac106126eede816f3d41
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/build/+/1614981
Reviewed-by: Dean Berris <dberris@chromium.org>
Commit-Queue: Owen Rodley <orodley@chromium.org>
diff --git a/scripts/slave/recipe_modules/codesearch/resources/add_kythe_metadata.py b/scripts/slave/recipe_modules/codesearch/resources/add_kythe_metadata.py
index cff7d2e..432999c 100755
--- a/scripts/slave/recipe_modules/codesearch/resources/add_kythe_metadata.py
+++ b/scripts/slave/recipe_modules/codesearch/resources/add_kythe_metadata.py
@@ -94,10 +94,8 @@
b64_metadata = base64.encodestring(json.dumps(metadata))
# base64.encodedstring returns multi-line output. This is fine by us, as we
- # want to wrap the comment anyway. The first line will be longer than the rest
- # since we add the magic comment string, but we don't care too much about
- # prettiness of the output.
- return '// Metadata comment ' + b64_metadata[:-1].replace('\n', '\n// ')
+ # want to wrap the comment anyway.
+ return '/* Metadata comment\n' + b64_metadata + '*/'
def main():
@@ -132,8 +130,12 @@
# metadata. In theory the metadata will be the same, since the file hasn't
# changed. However, if this script has changed we want the output from
# the new version rather than the old version, as it might be different.
- comment_pos = contents.find('\n// Metadata comment')
- if comment_pos != -1:
+ #
+ # We search for both '//' and '/*' comment prefixes, to match both the
+ # current output of this script, and the output from the previous version.
+ match = re.search('\n/[/*] Metadata comment', contents)
+ if match is not None:
+ comment_pos = match.start()
if opts.verbose:
print 'Clearing existing metadata from %s' % filename
f.seek(comment_pos)
diff --git a/scripts/slave/recipe_modules/codesearch/resources/add_kythe_metadata_test.py b/scripts/slave/recipe_modules/codesearch/resources/add_kythe_metadata_test.py
index 59cb2a1..d2fa3ac 100755
--- a/scripts/slave/recipe_modules/codesearch/resources/add_kythe_metadata_test.py
+++ b/scripts/slave/recipe_modules/codesearch/resources/add_kythe_metadata_test.py
@@ -188,16 +188,20 @@
def testFormatMetadataSingleLine(self):
- self.assertEqual('// Metadata comment eyJmb28iOiAiYmFyIn0=',
+ self.assertEqual('/* Metadata comment\neyJmb28iOiAiYmFyIn0=\n*/',
add_kythe_metadata._FormatMetadata({'foo': 'bar'}))
def testFormatMetadataMultiLine(self):
- self.assertEqual('// Metadata comment WyJhIiwgImEiLCAiYSIsICJhIiwgImEiLCA' +
- 'iYSIsICJhIiwgImEiLCAiYSIsICJhIiwgImEiLCAi\n' +
- '// YSIsICJhIiwgImEiLCAiYSIsICJhIiwgImEiLCAiYSIsICJhIiwg' +
- 'ImEiLCAiYSIsICJhIiwgImEi\n' +
- '// LCAiYSIsICJhIiwgImEiLCAiYSIsICJhIiwgImEiLCAiYSJd',
- add_kythe_metadata._FormatMetadata(['a'] * 30))
+ self.assertEqual(
+ '/* Metadata comment\n' +
+ 'WyJhIiwgImEiLCAiYSIsICJhIiwgImEiLCAiYSIsICJhIiwgImEiLCAiYSIsICJhIiwg' +
+ 'ImEiLCAi\n' +
+ 'YSIsICJhIiwgImEiLCAiYSIsICJhIiwgImEiLCAiYSIsICJhIiwgImEiLCAiYSIsICJh' +
+ 'IiwgImEi\n' +
+ 'LCAiYSIsICJhIiwgImEiLCAiYSIsICJhIiwgImEiLCAiYSJd\n' +
+ '*/',
+ add_kythe_metadata._FormatMetadata(['a'] * 30))
+
if __name__ == '__main__':
unittest.main()