Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thanks for writing this, it's very impressive! #1

Open
GavinRay97 opened this issue Jun 6, 2021 · 2 comments
Open

Thanks for writing this, it's very impressive! #1

GavinRay97 opened this issue Jun 6, 2021 · 2 comments

Comments

@GavinRay97
Copy link

GavinRay97 commented Jun 6, 2021

I have just built the lib and seen the generated output in build/examples/geom, this is really impressive!
The code is fairly concise + easy to read as well 😃

One question I had from browsing the source -- I notice you use an ASTConsumer instead of a RecursiveASTVisitor. Is this easier?

Have been investigating doing some codegen from C++ headers and would be curious to hear your opinion

@spaceotter
Copy link
Owner

I'm not some kind of expert on libclang, I had not heard of RecursiveASTVisitor so thanks for the tip. The project already explores the declarations recursively in the specific way it needs to so I'm not sure I would change it. Note that the FrontendAction uses an ASTConsumer - if you don't use the FrontendAction you may end up with the headache of setting up a compiler instance manually (see here) or it won't find the standard system headers. In terms of developing with clang I haven't found a better approach than just using your IDE on the whole project and poking through the code, and experimenting. The documentation is too sparse to be of much use.

@GavinRay97
Copy link
Author

Ahh got it -- I am in much the same boat. The documentation I agree is terrible.
You're pretty much left to your devices, and even searching online you cannot find much =(

You might find this useful though, this is the a class where you declare which kinds of nodes you want to visit and it extends FrontendAction with a recursive visitor for them that you can choose to either break/continue on each node:

https://clang.llvm.org/docs/RAVFrontendAction.html

class FindNamedClassVisitor
  : public RecursiveASTVisitor<FindNamedClassVisitor> {
public:
  explicit FindNamedClassVisitor(ASTContext *Context)
    : Context(Context) {}

  bool VisitCXXRecordDecl(CXXRecordDecl *Declaration) {
    if (Declaration->getQualifiedNameAsString() == "n::m::C") {
      FullSourceLoc FullLocation = Context->getFullLoc(Declaration->getBeginLoc());
      if (FullLocation.isValid())
        llvm::outs() << "Found declaration at "
                     << FullLocation.getSpellingLineNumber() << ":"
                     << FullLocation.getSpellingColumnNumber() << "\n";
    }
    return true;
  }

private:
  ASTContext *Context;
};

class FindNamedClassConsumer : public clang::ASTConsumer {
public:
  explicit FindNamedClassConsumer(ASTContext *Context)
    : Visitor(Context) {}

  virtual void HandleTranslationUnit(clang::ASTContext &Context) {
    Visitor.TraverseDecl(Context.getTranslationUnitDecl());
  }
private:
  FindNamedClassVisitor Visitor;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants