Skip to content

Instantly share code, notes, and snippets.

@schwarzschild-radius
Last active July 23, 2019 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schwarzschild-radius/8986bd5dc734183727291b3f3aae4c53 to your computer and use it in GitHub Desktop.
Save schwarzschild-radius/8986bd5dc734183727291b3f3aae4c53 to your computer and use it in GitHub Desktop.
bool type is represented as "_Bool" in the AST

The following code reproduces the behavior that I get

#include "clang/AST/ASTConsumer.h"
  #include "clang/AST/RecursiveASTVisitor.h"
  #include "clang/Frontend/CompilerInstance.h"
  #include "clang/Frontend/FrontendAction.h"
  #include "clang/Tooling/Tooling.h"

  using namespace clang;

  class FindNamedClassVisitor
    : public RecursiveASTVisitor<FindNamedClassVisitor> {
  public:
    explicit FindNamedClassVisitor(ASTContext *Context)
      : Context(Context) {}
   
    bool VisitVarDecl(VarDecl *var_decl){
      var_decl->dump();
      var_decl->getType().dump();
      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;
  };

  class FindNamedClassAction : public clang::ASTFrontendAction {
  public:
    virtual std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
      clang::CompilerInstance &Compiler, llvm::StringRef InFile) {
      return std::unique_ptr<clang::ASTConsumer>(
          new FindNamedClassConsumer(&Compiler.getASTContext()));
    }
  };

  int main(int argc, char **argv) {
    if (argc > 1) {
      clang::tooling::runToolOnCode(new FindNamedClassAction, argv[1]);
    }
  }

Makefile

clang++ source.cpp -o source -std=c++17 -I `llvm-config --includedir` `llvm-config --libs --ldflags` -lclangFrontend -lclangDriver -lclangSerialization -lclangParse -lclangSema -lclangAnalysis -lclangAST -lclangBasic -lclangEdit -lclangTooling -lclangLex -lclangASTMatchers -lclangRewrite -lclang

I get the following AST Dump for bool a = false;

VarDecl 0x389cb00 <input.cc:1:1, col:10> col:6 a 'bool' cinit
`-CXXBoolLiteralExpr 0x389cbb0 <col:10> 'bool' false
BuiltinType 0x3860e30 '_Bool'

I tried setting PrintingPolicy. This is for Bool = 0

VarDecl 0x1b6bb00 <input.cc:1:1, col:10> col:6 a '_Bool' cinit
`-CXXBoolLiteralExpr 0x1b6bbb0 <col:10> '_Bool' false
BuiltinType 0x1b2fe30 '_Bool'

and This is for Bool = 1

VarDecl 0x24dcb00 <input.cc:1:1, col:10> col:6 a 'bool' cinit
`-CXXBoolLiteralExpr 0x24dcbb0 <col:10> 'bool' false
BuiltinType 0x24a0e30 '_Bool'

I get _Bool from QualType::getAsString for both the cases. I also passed -x c++ to the tool and I get the same behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment