<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jul 2, 2017 at 12:40 PM, Artem Dergachev <span dir="ltr"><<a href="mailto:noqnoqneo@gmail.com" target="_blank">noqnoqneo@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Uhm, why does this thing return `const Decl &` rather than `const Decl *`? Anyway, whatever, never mind.<br>
<br>
<br>
You're crashing in...<br>
<br>
  cast<FunctionDecl>(&EN->getCod<wbr>eDecl())<br>
<br>
...because this Decl returned by `EN->getCodeDecl()` isn't a FunctionDecl.<br>
<br>
<br>
You can easily see this from the backtrace:<br>
<br>
Assertion failed: (isa<X>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file (...censored...)/llvm/include/<wbr>llvm/Support/Casting.h, line 254.<br>
0  libLLVMSupport.dylib                 0x00000001148fb8fc llvm::sys::PrintStackTrace(llv<wbr>m::raw_ostream&) + 60<br>
1  libLLVMSupport.dylib                 0x00000001148fbec9 PrintStackTraceSignalHandler(v<wbr>oid*) + 25<br>
2  libLLVMSupport.dylib                 0x00000001148f7b59 llvm::sys::RunSignalHandlers() + 425<br>
3  libLLVMSupport.dylib                 0x00000001148fc212 SignalHandler(int) + 354<br>
4  libsystem_platform.dylib             0x00007fffd872cb3a _sigtramp + 26<br>
5  libclangStaticAnalyzerCore.dyl<wbr>ib     0x0000000122060c7b llvm::alignAddr(void const*, unsigned long) + 43<br>
6  libsystem_c.dylib                    0x00007fffd85b1420 abort + 129<br>
7  libsystem_c.dylib                    0x00007fffd8578893 basename_r + 0<br>
8  libclangStaticAnalyzerCheckers<wbr>.dylib 0x00000001202e6fe7 llvm::cast_retty<clang::Functi<wbr>onDecl, clang::Decl const*>::ret_type llvm::cast<clang::FunctionDecl<wbr>, clang::Decl const>(clang::Decl const*) + 103<br>
9  libclangStaticAnalyzerCheckers<wbr>.dylib 0x00000001208b7aa6 isAnnotatedToSkipDiagnostics(c<wbr>lang::ento::ExplodedNode const*) + 38<br>
(...more stuff...)<br>
<br>
You're calling llvm::cast<> (frame 8) from your new function (frame 9), the only cast you have is `cast<FunctionDecl>(&EN->getCo<wbr>deDecl())`, and the assertion message says that the argument is of incompatible type.<br>
<br>
<br>
If you want to know what specific decl causes a problem, you can print it in the debugger:<br>
<br>
$ lldb -- (...censored...)/debug/./bin/c<wbr>lang -cc1 -internal-isystem (...censored...)/debug/lib/cla<wbr>ng/5.0.0/include -nostdsysteminc -analyze -analyzer-constraints=range -analyzer-checker=core,osx.coc<wbr>oa.RetainCount,osx.cocoa.Deall<wbr>oc,debug.ExprInspection -analyzer-store=region -verify -Wno-objc-root-class (...censored...)/llvm/tools/cl<wbr>ang/test/Analysis/properties.m<br>
<br>
(lldb) bt<br>
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT<br>
    frame #0: 0x00007fffd864bd42 libsystem_kernel.dylib`__pthre<wbr>ad_kill + 10<br>
    frame #1: 0x00007fffd8739787 libsystem_pthread.dylib`pthrea<wbr>d_kill + 90<br>
    frame #2: 0x00007fffd85b1420 libsystem_c.dylib`abort + 129<br>
    frame #3: 0x00007fffd8578893 libsystem_c.dylib`__assert_rtn + 320<br>
    frame #4: 0x0000000114983fe7 libclangStaticAnalyzerCheckers<wbr>.dylib`llvm::cast_retty<clang:<wbr>:FunctionDecl, clang::Decl const*>::ret_type llvm::cast<clang::FunctionDecl<wbr>, clang::Decl const>(Val=0x00000001170ab5c0) at Casting.h:254<br>
  * frame #5: 0x0000000114f54aa6 libclangStaticAnalyzerCheckers<wbr>.dylib`isAnnotatedToSkipDiagno<wbr>stics(EN=0x0000000118815ee0) at RetainCountChecker.cpp:1904<br>
(...more stuff...)<br>
<br>
(lldb) f 5<br>
frame #5: 0x0000000114f54aa6 libclangStaticAnalyzerCheckers<wbr>.dylib`isAnnotatedToSkipDiagno<wbr>stics(EN=0x0000000118815ee0) at RetainCountChecker.cpp:1904<br>
   1901<br>
   1902    bool<br>
   1903    isAnnotatedToSkipDiagnostics(c<wbr>onst ExplodedNode *EN) {<br>
-> 1904      const FunctionDecl *FD = cast<FunctionDecl>(&EN->getCod<wbr>eDecl());<br>
   1905      const IdentifierInfo *II = FD->getIdentifier();<br>
   1906<br>
   1907      if (II) {<br>
<br>
(lldb) p EN->getCodeDecl().dump()<br>
ObjCMethodDecl 0x1170ab5c0 <(...censored...)/llvm/tools/c<wbr>lang/test/Analysis/properties.<wbr>m:559:1, line:563:1> line:559:1 - testRetainAndRelease 'void'<br>
(...more stuff...)<br></blockquote><div><br></div><div>Thanks a ton for this, Dr. Artem. This really helped. </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Note that frames are shifted in the debugger because it stops on the moment when the assertion has failed, so we're on frame 5 instead of frame 9 now.<br>
<br>
If you use gdb instead of lldb, just replace "lldb --" with "gdb --args" in the first command, the rest is the same. If you use an IDE, then you should know better how to debug this.<br>
<br>
<br>
So in this case it's ObjCMethodDecl, and Objective-C methods apparently aren't functions. I'd also worry about BlockDecl which may also appear here.<br>
<br>
You should use dyn_cast<> when you're not sure if the cast succeeds - it'd return nullptr instead of crashing, and you can handle it.</blockquote><div>Okay, I'll try using dyn_cast<>. </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><br>
<br>
<br>
On 7/1/17 10:12 AM, Malhar Thakkar via cfe-dev wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
Dear all,<br>
<br>
I am currently trying to suppress diagnostics emitted if the function under consideration has a certain annotate attribute ("check_attribute_annotate" in this case).<br>
<br>
Hence, for that, I added the following piece of code to RetainCountChecker.cpp but it is resulting in some assertion failures while performing make -j4 check-clang-analysis.<br>
<br>
<br>
<br>
diff --git a/lib/StaticAnalyzer/Checkers/<wbr>RetainCountChecker.cpp b/lib/StaticAnalyzer/Checkers/<wbr>RetainCountChecker.cpp<br>
index 89b1291..9f367be 100644<br>
--- a/lib/StaticAnalyzer/Checkers/<wbr>RetainCountChecker.cpp<br>
+++ b/lib/StaticAnalyzer/Checkers/<wbr>RetainCountChecker.cpp<br>
@@ -1894,6 +1894,20 @@ static bool isSynthesizedAccessor(const StackFrameContext *SFC) {<br>
return SFC->getAnalysisDeclContext()-<wbr>>isBodyAutosynthesized();<br>
 }<br>
+bool<br>
+isAnnotatedToSkipDiagnostics(<wbr>const ExplodedNode *EN) {<br>
+ const FunctionDecl *FD = cast<FunctionDecl>(&EN->getCod<wbr>eDecl());<br>
+ const IdentifierInfo *II = FD->getIdentifier();<br>
+<br>
+ if (II) {<br>
+ for (const auto *Ann : FD->specific_attrs<AnnotateAtt<wbr>r>()){<br>
+ if (Ann->getAnnotation() == "check_attribute_annotate")<br>
+ return true;<br>
+    }<br>
+  }<br>
+ return false;<br>
+}<br>
+<br>
 std::shared_ptr<PathDiagnosti<wbr>cPiece><br>
 CFRefReportVisitor::<wbr>VisitNode(const ExplodedNode *N, const ExplodedNode *PrevN,<br>
           BugReporterContext &BRC, BugReport &BR) {<br>
@@ -3345,11 +3359,13 @@ void RetainCountChecker::processNon<wbr>LeakError(ProgramStateRef St,<br>
   }<br>
   assert(BT);<br>
-  auto report = std::unique_ptr<BugReport>(<br>
- new CFRefReport(*BT, C.getASTContext().getLangOpts(<wbr>), C.isObjCGCEnabled(),<br>
-    SummaryLog, N, Sym));<br>
-  report->addRange(ErrorRange);<br>
-  C.emitReport(std::move(report)<wbr>);<br>
+ if (!isAnnotatedToSkipDiagnostics<wbr>(N)){<br>
+  auto report = std::unique_ptr<BugReport>(<br>
+ new CFRefReport(*BT, C.getASTContext().getLangOpts(<wbr>), C.isObjCGCEnabled(),<br>
+                      SummaryLog, N, Sym));<br>
+  report->addRange(ErrorRange);<br>
+  C.emitReport(std::move(report)<wbr>);<br>
+  }<br>
 }<br>
//===-------------------------<wbr>------------------------------<wbr>---------------===//<br>
<br>
More specifically, the code highlighted in red above is causing the two assertion failures mentioned below.<br>
<br>
The following test-cases are the unexpected failures:<br>
<br></div></div>
  * Analysis/inlining/RetainCountE<wbr>xamples.m<br>
  * Analysis/properties.m<br>
<br>
*Note:* The above diff is a subset of the code that I have changed. I added the check *if (!isAnnotatedToSkipDiagnostics<wbr>(N))* before every invocation to *emitReport* to suppress raising warnings for functions having the annotate attribute *check_attribute_annotate*. It seemed to work for manually made test-cases but clearly there is something wrong with my methodology.<span class=""><br>
<br>
<br>
Could anyone please tell me the reason behind these assertion failures?<br>
<br>
<br>
Thank you.<br>
<br>
<br>
Regards,<br>
Malhar Thakkar<br></span>
ᐧ<br>
<br>
<br>
______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
</blockquote>
<br>
</blockquote></div><br></div></div><div hspace="streak-pt-mark" style="max-height:1px"><img alt="" style="width:0px;max-height:0px;overflow:hidden" src="https://mailfoogae.appspot.com/t?sender=aY3MxM2IxMDMxQGlpdGguYWMuaW4%3D&type=zerocontent&guid=8a80bad2-0fbb-4e9b-a275-7b362344ddab"><font color="#ffffff" size="1">ᐧ</font></div>