[cfe-users] dissecting the type of a VarDecl

2016-09-16 Thread folkert via cfe-users
Hi,

I have a VarDecl instance for which I want to dissect the type.
E.g. a const int a would be a const, an int and the name a /
std::vector would be namespace std, vector and so on.

The first one is easy but the second one: I have no idea where to begin.

Sofar I have the following:

#include 
#include 
#include 

using namespace clang;

void printQT(ASTContext *const Context, const QualType x)
{
if (x.isNull())
return;

QualType type = x.getNonReferenceType();

for(;!type.isNull();) {
if (type.hasQualifiers()) {
Qualifiers q = type.getQualifiers();

if (q.hasConst())
printf("const ");

if (q.hasVolatile())
printf("volatile ");

if (q.hasRestrict())
printf("restrict ");
}

const Type *t = type.getTypePtr();

if (!t) {
printf("null?\n");
break;
}
else if (t -> isPointerType())
printf("* ");
else if (t -> isFundamentalType()) {
std::string curType = 
type.getUnqualifiedType().getAsString();

printf("[F]%s ", curType.c_str());
break; // should be last entry in this chain
}
else {
std::string curType = 
type.getUnqualifiedType().getAsString();
// dissect

printf("%s ", curType.c_str());
break; // should be last entry in this chain
}

type = type->getPointeeType();
}

printf("\n");
}

int main(int argc, char **argv)
{
FILE *fh = fopen("example.cpp", "r");

fseek(fh, 0, SEEK_END);
size_t len = ftell(fh);
fseek(fh, 0, SEEK_SET);
char *buf = (char *)malloc(len + 1);
fread(buf, 1, len, fh);
buf[len] = 0x00;
fclose(fh);

llvm::Twine code(buf);

std::vector arguments;
arguments.push_back("-resource-dir=/usr/local/llvm/lib/clang/4.0.0");

std::unique_ptr au = 
clang::tooling::buildASTFromCodeWithArgs(code, arguments, 
llvm::Twine("example.cpp"));

for(clang::ASTUnit::top_level_iterator dit = au -> top_level_begin(); 
dit != au -> top_level_end(); dit++)
{
if (isa(*dit)) {
const VarDecl *vd = static_cast(*dit);
printQT(&au -> getASTContext(), vd -> getType());
}
}

return 0;
}



Folkert van Heusden
___
cfe-users mailing list
cfe-users@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users


[cfe-users] Fwd: [cfe-dev] matcher for obj-c variable declarations.

2016-09-16 Thread Alfred Zien via cfe-users
cfe-dev was wrong list may be?

> Hi to everyone!
> 
> I'm new to clang and libtooling, and I'm making some cheker tool that emits a 
> error if any obj-c variable declaration doesn't have nullability specifier.
> 
> I have code like this
> 
>   A* a = [[A alloc] init];
>   B* _Null_unspecified b = [[B alloc] init];
>   C* _Nullable c = [[C alloc] init];
>   D* _Nonnull d = [[D alloc] init];
> 
> Now, I want to create a matcher to match variables declaration.
> 
> Currently I have something like this:
> StatementMatcher NullUnspecifiedMatcher =
>   declStmt(hasSingleDecl(varDecl(hasType(objcObjectPointerType()
> 
> There is objcObjectPointerType() but it matches only a (is it a bug?). There 
> are some pointers/references matchers, but they don't work on those variable 
> declarations at all. Also, I tried isAnyPointer() but it works on any 
> pointer, obj-c or not.
> 
> I tried type() to ensure, that I'm doing everything correctly, and it works.
> 
> So, how can I match only obj-c variable declarations, with or without 
> nullability specifier? 
> 
> Thanks for any help!
> ___
> cfe-dev mailing list
> cfe-...@lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

___
cfe-users mailing list
cfe-users@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users