[cfe-dev] (Kind of) Extending the Clang AST

Gabor Kozar kozargabor at gmail.com
Mon Sep 30 10:18:07 PDT 2013


Hi,



(Note: all of the code shown below is incomplete and totally untested -
not even sure if it compiles.)



I am developing a Static Analyzer plug-in for Clang 3.3. I have created
a class like this:



class ContainerDecl : public NamedDecl

{

public:

    static bool classof(const Decl* decl);



    static bool isContainerDecl(const NamedDecl* decl);

    static const ContainerDecl* tryCreateFrom(const NamedDecl* decl);



    const NamedDecl* getWrappedDecl() const;

    bool isValidContainer() const;



    QualType getIteratorType() const;

    QualType getItemValueType() const;



protected:

    ContainerDecl(const NamedDecl* decl);



private:

    ...

};



As you can see, this is basically a wrapper around a NamedDecl, and
provides convenience methods for getting information about the
container the NamedDecl describes.



The motivation comes from writing numerous Static Analyzer checkers for
STL containers, and these usually utilize AST matchers. I want to be
able to write a matcher expression like this:



containerDecl(hasItemValueType(hasDeclaration(namedDecl(hasName("std::a
uto_ptr")))))



... in order to detect issues with the now-depracted std::auto_ptr-s
being put in a container, and then e.g. std::sort()-ed. (This is not a
complete matcher expression of course.)



With the above class, it is quite easy to do this:



typedef internal::Matcher<ContainerDecl> ContainerDeclMatcher;



AST_MATCHER_P(NamedDecl, containerDecl, ContainerDeclMatcher,
innerMatcher)

{

    // TODO: lifetime management? if we don't need the containerDecl
any longer, we should delete it

    const ContainerDecl* containerDecl =
ContainerDecl::tryCreateFrom(&Node);

    if(!containerDecl) return false;



    return innerMatcher.matches(*containerDecl, Finder, Builder);

}



AST_MATCHER_P(ContainerDecl, hasItemValueType, TypeMatcher,
innerMatcher)

{

    return innerMatcher.matches(Node.getItemValueType(), Finder,
Builder);

}



AST_MATCHER_P(ContainerDecl, hasIteratorType, TypeMatcher,
innerMatcher)

{

    return innerMatcher.matches(Node.getIteratorType(), Finder,
Builder);

}



What I am not sure about, is how to handle the lifetime of a
ContainerDecl, since this isn't strictly part of Clang's AST - it is
just basically a wrapper around a NamedDecl. ContainerDecl objects in
general will be short-lived, compared to e.g. the NamedDecl objects
they wrap.



I noticed that there is ASTContext::Allocate and Deallocate, but I am
not sure whether I should use it. Thoughts on this?



I am also not entirely happy about having to inherit from NamedDecl,
but otherwise - as far as I know - I cannot implement the above
matchers. Is this even the correct approach?



The other problem is making ContainerDecl play well with llvm::dyn_cast
and the like. Right now I have the following implementation for
ContainerDecl::classof:



static bool ContainerDecl::classof(const Decl* decl)

{

    return NamedDecl::classof(decl) &&
isContainerDecl(static_cast<const NamedDecl*>(decl));

}



Is this correct?



Also, do you think this is something that could be integrated into
Clang at some point? I'd happy to contribute the code once it is
working, and my boss agrees to have this code open-sourced.



Thanks!



--
Gábor Kozár -- ShdNx
kozargabor at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130930/6be591b3/attachment.html>


More information about the cfe-dev mailing list