Attempting to get type from typedef struct causes crash

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Attempting to get type from typedef struct causes crash

David Blaikie via cfe-dev

Hi,

 

I am trying to write a custom clang-tidy check (I want to check for the use of CAdapt<>) but have been struggling with the following issue: I am trying to make an AST matcher which checks the string representation of the type of a VarDecl - but when it runs on certain code it crashes and I can’t figure out why. The issue seems to be some interaction between QualType::getAsString() and typedef struct variables, (there may be other code which crashes it but this is what I have been able to find). The issue can be reproduced with the following custom clang-tidy check (apologies if there is a more sensible way to make a minimal repro than a clang-tidy check):

 

//-----------------------------------------------------------

#include "CrashingCheck.h"

#include "clang/AST/ASTContext.h"

#include "clang/ASTMatchers/ASTMatchFinder.h"

 

using namespace clang::ast_matchers;

 

namespace clang {

namespace tidy {

namespace misc {

 

    namespace {

        AST_MATCHER(VarDecl, isMatch) {

            Node.getTypeSourceInfo()->getType().getAsString();

            return false;

        }

    }

   

    void CrashingCheck::registerMatchers(MatchFinder *Finder) {

        Finder->addMatcher(varDecl(isMatch()).bind("varDecl"), this);

    }

 

    void CrashingCheck::check(const MatchFinder::MatchResult &Result) {}

}}}

//-----------------------------------------------------------

 

Which crashes when run on:

 

//-----------------------------------------------------------

typedef struct foo{} foo;

 

int main(){

    foo f;

    return 0;

}

//-----------------------------------------------------------

 

Is there some restriction on when it is OK to use getAsString() that I am not understanding? Also, if there is a better way to write something like this then do let me know, I’m pretty new to this.

 

Thanks,

Stuart


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev