<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">Hello Li,<br>
<br>
What you're doing is mostly fine. I can only add some potentially
useful info inline.<br>
<br>
14.02.2018 23:04, Li Kan via cfe-dev пишет:<br>
</div>
<blockquote type="cite"
cite="mid:CABANeYi6HuBi1S_b027vMQxJxZa4YCjtqObRXeKc5Tp__vptOQ@mail.gmail.com">
<div dir="ltr">Hi folks,<br>
<br>
If this is not the correct mailing list for this question,
please let me know.<br>
</div>
</blockquote>
You're in the right place. Welcome :)<br>
<br>
<blockquote type="cite"
cite="mid:CABANeYi6HuBi1S_b027vMQxJxZa4YCjtqObRXeKc5Tp__vptOQ@mail.gmail.com">
<div dir="ltr">I am trying to write a static analyzer for code
base of my current job. The particular static analyzer I am
writing involves a particular class of my company's code base.
Let's say it is T. I want to write a checker to ensure T.ok() is
called before T.value() is called.<br>
<br>
Static analyzer is perfect for this type of check as it requires
path sensitive checks. When I trying to write the checker, I
basically checks for pre-call and post-call. I want to tell if a
CallEvent is T.ok() and T.value(). Currently what I am doing is:<br>
1. From CallEvent, I cast to CXXMemberCall, and getOriginExpr(),
then call getMethodDecl().<br>
</div>
</blockquote>
You can also try to do the following chain:
dyn_cast_or_null<CXXMethodDecl>(CallEvent.getDecl()) to obtain
CXXMethodDecl.<br>
<br>
<blockquote type="cite"
cite="mid:CABANeYi6HuBi1S_b027vMQxJxZa4YCjtqObRXeKc5Tp__vptOQ@mail.gmail.com">
<div dir="ltr">2. From CXXMethodDecl, I first call getThisType(),
then call getAsCXXRecordDecl(), then getQualifiedNameAsString(),
and compare with qualified name of T, to make sure it is member
of T.<br>
</div>
</blockquote>
To obtain parent class declaration from CXXMethodDecl, you can use
getParent() method. So, the pseudocode will look like
"MethodDecl->getParent()->getQualifiedNameAsString()".<br>
<br>
<blockquote type="cite"
cite="mid:CABANeYi6HuBi1S_b027vMQxJxZa4YCjtqObRXeKc5Tp__vptOQ@mail.gmail.com">
<div dir="ltr">3. From CXXMethodDecl, I call getNameAsString() to
get the method name, and compare them with "ok" and "value".<br>
</div>
</blockquote>
This looks OK. But it can be useful to know that
getQualifiedNameAsString() for CXXMethodDecl will also include
parent name. So, you can just check if
MethodDecl->getQualifiedNameAsString() is equal to "T::value" or
"T::ok".<br>
<br>
<blockquote type="cite"
cite="mid:CABANeYi6HuBi1S_b027vMQxJxZa4YCjtqObRXeKc5Tp__vptOQ@mail.gmail.com">
<div dir="ltr">It works, but it looks complicated, and involves
string comparison, which I assume is slow. Is there a easier
way, blessed by clang static analyzer official teams, that tell
if a MethodDecl, or CXXRecordDecl, is the function or class I am
interested in?<br>
<br>
The ideal way is, there is one-time call to lookup the
MethodDecl for T::ok() and T::value(), and CXXRecordDecl for T.
Then ever since then, I just need to compare the pointers.<br>
</div>
</blockquote>
This is possible. You can just launch a simple matcher against AST:<br>
cxxMethodDecl(hasName("::T::value"))<br>
and get CXXMethodDecl you need and compare with it later.<br>
<br>
<blockquote type="cite"
cite="mid:CABANeYi6HuBi1S_b027vMQxJxZa4YCjtqObRXeKc5Tp__vptOQ@mail.gmail.com">
<div dir="ltr">Another way is, the first time I found it is T,
T::ok() or T::value(), I save the pointer of CXXRecordDecl or
MethodDecl, and use the pointers later. Is this the reliable
(assume I use the pointer canonical decl will not change) and
blessed way to do it? If it is, is my steps above the correct
and simplest way to determine it is T::ok or T::value? Is there
some better and more reliable way?<br>
</div>
</blockquote>
I think it is reliable enough: pointers to canonical declarations
don't change in AST.<br>
<blockquote type="cite"
cite="mid:CABANeYi6HuBi1S_b027vMQxJxZa4YCjtqObRXeKc5Tp__vptOQ@mail.gmail.com">
<div dir="ltr"><br>
Thanks.<br>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
cfe-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a>
</pre>
</blockquote>
<p><br>
</p>
<pre class="moz-signature" cols="72">--
Best regards,
Aleksei Sidorin,
SRR, Samsung Electronics
</pre>
</body>
</html>