<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Clang does do a good job keeping track of source locations, and the SourceManager class can translate SourceLocations into line numbers. What you're actually trying to do is a bit harder, though.</div><div><br></div><div>If you just want to leverage the compiler for this, C11 already has this natively in the form of static_assert:</div><div><br></div><div>// clang -std=c11 main.c</div><div>#include <assert.h></div><div><br></div><div>int main()</div><div>{</div><div>  int x = 10;</div><div>  static_assert(x > 0, "something is seriously wrong");</div><div>  return 0;</div><div>}</div><div><br></div><div>C++11 has the same thing, except you don't need to include <assert.h>.</div><div><br></div><div>If you want to do <i>path-sensitive</i> assertions, however, your job is much harder. A tool like the static analyzer has to assume it doesn't know everything, and so when it sees assert(a <= b), it takes that as a new fact. Now consider code like this:</div><div><br></div><div>int a = hasA ? 4 : 0;</div><div>int b = hasB ? 5 : 0;</div><div>assert(a <= b);</div><div><br></div><div>There are four possible cases here, but the assertion is false on one of them (hasA && !hasB). Should you get a warning there, or is it an "implicit assertion" that you will never have hasA without hasB? The static analyzer assumes the latter. Figuring out that you meant the former is difficult.</div><div><br></div><div>You <i>could</i> have another type of assertion that says "I think this should be statically provable along any path", and then have the analyzer warn when these assertions are violated, but I'm not sure adding yet another kind of assertion is a good idea for a codebase.</div><div><br></div><div>Anyway, that's the state of things. Sorry it's not a more encouraging answer!</div><div>Jordan</div><div><br></div><br><div><div>On May 13, 2013, at 4:50 , Rajendra <<a href="mailto:rks@cse.iitb.ac.in">rks@cse.iitb.ac.in</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">I want to verify some user defined assertion in C code. e.g. <br><br>int main()<br>{<br>  int x, y;<br>  x = 10;<br>  MYASSERT1: x>0; <br>  y = 0;<br>  MYASSERT2: x==0; <br>  return 0;<br>}<br><br>How does clang API (or any other tool) help to achieve this? besides I want<br>to keep track of line number in original C code. <br><br>All help is appreciated. <br><br><br><br>--<br>View this message in context: <a href="http://clang-developers.42468.n3.nabble.com/adding-assertion-in-C-code-and-verifying-using-clang-API-tp4032068.html">http://clang-developers.42468.n3.nabble.com/adding-assertion-in-C-code-and-verifying-using-clang-API-tp4032068.html</a><br>Sent from the Clang Developers mailing list archive at <a href="http://Nabble.com">Nabble.com</a>.<br>_______________________________________________<br>cfe-dev mailing list<br><a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev<br></blockquote></div><br></body></html>