[clang] f72f122 - Update python script per review comments
Stephen Kelly via cfe-commits
cfe-commits at lists.llvm.org
Sun Mar 14 08:37:58 PDT 2021
Author: Stephen Kelly
Date: 2021-03-14T15:37:30Z
New Revision: f72f122feebe7f980c22ed4a7e04fc274ce2c976
URL: https://github.com/llvm/llvm-project/commit/f72f122feebe7f980c22ed4a7e04fc274ce2c976
DIFF: https://github.com/llvm/llvm-project/commit/f72f122feebe7f980c22ed4a7e04fc274ce2c976.diff
LOG: Update python script per review comments
Wrap to 80 cols, introduce main() function and use argparse instead of
optparse.
Added:
Modified:
clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py
Removed:
################################################################################
diff --git a/clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py b/clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py
index aafae2c15b29..0740db0c7629 100755
--- a/clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py
+++ b/clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py
@@ -5,40 +5,7 @@
import sys
import json
-from optparse import OptionParser
-
-parser = OptionParser()
-parser.add_option('--json-input-path',
- help='Read API description from FILE', metavar='FILE')
-parser.add_option('--output-file', help='Generate output in FILEPATH',
- metavar='FILEPATH')
-parser.add_option('--empty-implementation', help='Generate empty implementation',
- action="store", type="int", metavar='FILEPATH')
-
-(options, args) = parser.parse_args()
-
-if options.empty_implementation:
- with open(os.path.join(os.getcwd(),
- options.output_file), 'w') as f:
- f.write("""
-namespace clang {
-namespace tooling {
-
-NodeLocationAccessors NodeIntrospection::GetLocations(clang::Stmt const *) {
- return {};
-}
-NodeLocationAccessors
-NodeIntrospection::GetLocations(clang::DynTypedNode const &) {
- return {};
-}
-} // namespace tooling
-} // namespace clang
-""")
- sys.exit(0)
-
-with open(options.json_input_path) as f:
- jsonData = json.load(f)
-
+import argparse
class Generator(object):
@@ -66,7 +33,8 @@ def GeneratePrologue(self):
def GenerateBaseGetLocationsDeclaration(self, CladeName):
self.implementationContent += \
"""
-void GetLocationsImpl(std::shared_ptr<LocationCall> const& Prefix, clang::{0} const *Object, SourceLocationMap &Locs,
+void GetLocationsImpl(std::shared_ptr<LocationCall> const& Prefix,
+ clang::{0} const *Object, SourceLocationMap &Locs,
SourceRangeMap &Rngs);
""".format(CladeName)
@@ -84,7 +52,8 @@ def GenerateSrcLocMethod(self, ClassName, ClassData):
for locName in ClassData['sourceLocations']:
self.implementationContent += \
"""
- Locs.insert(LocationAndString(Object.{0}(), std::make_shared<LocationCall>(Prefix, "{0}")));
+ Locs.insert(LocationAndString(Object.{0}(),
+ std::make_shared<LocationCall>(Prefix, "{0}")));
""".format(locName)
self.implementationContent += '\n'
@@ -93,7 +62,8 @@ def GenerateSrcLocMethod(self, ClassName, ClassData):
for rngName in ClassData['sourceRanges']:
self.implementationContent += \
"""
- Rngs.insert(RangeAndString(Object.{0}(), std::make_shared<LocationCall>(Prefix, "{0}")));
+ Rngs.insert(RangeAndString(Object.{0}(),
+ std::make_shared<LocationCall>(Prefix, "{0}")));
""".format(rngName)
self.implementationContent += '\n'
@@ -105,16 +75,6 @@ def GenerateFiles(self, OutputFile):
OutputFile), 'w') as f:
f.write(self.implementationContent)
- def GenerateTrivialBaseGetLocationsFunction(self, CladeName):
- MethodReturnType = 'NodeLocationAccessors'
-
- Signature = \
- 'GetLocations(clang::{0} const *Object)'.format(CladeName)
-
- self.implementationContent += \
- '{0} NodeIntrospection::{1} {{ return {{}}; }}'.format(MethodReturnType,
- Signature)
-
def GenerateBaseGetLocationsFunction(self, ASTClassNames, CladeName):
MethodReturnType = 'NodeLocationAccessors'
@@ -129,7 +89,8 @@ def GenerateBaseGetLocationsFunction(self, ASTClassNames, CladeName):
""".format(CladeName)
self.implementationContent += \
- 'void {0} {{ GetLocations{1}(Prefix, *Object, Locs, Rngs);'.format(ImplSignature,
+ 'void {0} {{ GetLocations{1}(Prefix, *Object, Locs, Rngs);'.format(
+ ImplSignature,
CladeName)
for ASTClassName in ASTClassNames:
@@ -180,29 +141,61 @@ def GenerateEpilogue(self):
}
'''
+def main():
+
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--json-input-path',
+ help='Read API description from FILE', metavar='FILE')
+ parser.add_argument('--output-file', help='Generate output in FILEPATH',
+ metavar='FILEPATH')
+ parser.add_argument('--empty-implementation',
+ help='Generate empty implementation',
+ action="store", type=int)
+
+ options = parser.parse_args()
+
+ if options.empty_implementation:
+ with open(os.path.join(os.getcwd(),
+ options.output_file), 'w') as f:
+ f.write("""
+namespace clang {
+namespace tooling {
+
+NodeLocationAccessors NodeIntrospection::GetLocations(clang::Stmt const *) {
+ return {};
+}
+NodeLocationAccessors
+NodeIntrospection::GetLocations(clang::DynTypedNode const &) {
+ return {};
+}
+} // namespace tooling
+} // namespace clang
+ """)
+ sys.exit(0)
+
+ with open(options.json_input_path) as f:
+ jsonData = json.load(f)
-g = Generator()
+ g = Generator()
-g.GeneratePrologue()
+ g.GeneratePrologue()
-if 'classesInClade' in jsonData:
- for (CladeName, ClassNameData) in jsonData['classesInClade'].items():
- g.GenerateBaseGetLocationsDeclaration(CladeName)
+ if 'classesInClade' in jsonData:
+ for (CladeName, ClassNameData) in jsonData['classesInClade'].items():
+ g.GenerateBaseGetLocationsDeclaration(CladeName)
- for (ClassName, ClassAccessors) in jsonData['classEntries'].items():
- if ClassAccessors:
- g.GenerateSrcLocMethod(ClassName, ClassAccessors)
+ for (ClassName, ClassAccessors) in jsonData['classEntries'].items():
+ if ClassAccessors:
+ g.GenerateSrcLocMethod(ClassName, ClassAccessors)
- for (CladeName, ClassNameData) in jsonData['classesInClade'].items():
- g.GenerateBaseGetLocationsFunction(ClassNameData, CladeName)
+ for (CladeName, ClassNameData) in jsonData['classesInClade'].items():
+ g.GenerateBaseGetLocationsFunction(ClassNameData, CladeName)
- g.GenerateDynNodeVisitor(jsonData['classesInClade'].keys())
-else:
- for CladeName in ['Stmt']:
- g.GenerateTrivialBaseGetLocationsFunction(CladeName)
+ g.GenerateDynNodeVisitor(jsonData['classesInClade'].keys())
- g.GenerateDynNodeVisitor([])
+ g.GenerateEpilogue()
-g.GenerateEpilogue()
+ g.GenerateFiles(options.output_file)
-g.GenerateFiles(options.output_file)
+if __name__ == '__main__':
+ main()
More information about the cfe-commits
mailing list