1

I am trying to create a refactoring tool that would allow me to get a syntax tree from an objective-c class so that I can change the structure of the class and output a different version of it that matches my criteria. I am looking at Clang's Libtooling to generate an AST and then take it from there, the issue I'm having is that I need to somehow make sure I provice all paths to all possible headers that are being imported from this source, and that's something I'd like to avoid.

I am wondering if there is a way to generate the AST for a class without having to for example provide paths for the framework containing the class definitions of the properties that the class I wanna refactor hold.

Ideally I would be able to get nodes in raw text of my source file containing things like properties, functions, etc... this way I'd be able to traverse that tree and change its structure to later on regenerate my source in the desired way.

jscs
  • 63,694
  • 13
  • 151
  • 195
Andrespch
  • 370
  • 3
  • 16
  • How about using AST Matcher + replacement ? – Booo Mar 09 '19 at 00:50
  • this is what I've tried so far, but I run into issues when using this strategy since the compiler fails to resolve the dependencies, so I was wondering if there is a way I can get the AST for the current source file without having to provide knowledge to the compiler about the sub-properties contained in my class, specially since I just need to restructure the tree, and regenerate the code. I'm hoping there is a parameter that will allow me to get the AST without much complication. Any ideas @boq? – Andrespch Mar 09 '19 at 10:06
  • not really... the interesting question to me is why Clang failed parsing your headers. Maybe this post is useful ? https://stackoverflow.com/a/45239905/5520012 – Booo Mar 09 '19 at 12:48
  • Thanks @boq, it does feel like that that I need, unfortunately, I can't seem to find a way to access that API from the class I'm using `ClangTool`. I'm trying to guide myself using this tutorial http://clang.llvm.org/docs/LibASTMatchersTutorial.html but it hadn't been very helpful so far! – Andrespch Mar 12 '19 at 16:43

1 Answers1

0

After doing more research I deveoped the understanding that what I was trying to do is not even possible as LibTooling based tools need syntactic and semantic information about a program. This information can be provided via a compile_commands.json file like stated on the documentation:

Clang Tooling needs a compilation database to figure out specific build options for each file. Currently it can create a compilation database from the compile_commands.json file

For Xcode projects, this file can be generated like this:

xcodebuild -project PROJECT_NAME.xcodeproj | xcpretty -r json-compilation-database --output compile_commands.json

you will need to install the xcpretty gem. (gem install xcpretty)

Source: https://clang.llvm.org/docs/HowToSetupToolingForLLVM.html

Andrespch
  • 370
  • 3
  • 16