<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">It is expected.  Global variables are infamous for being extremely hard to analyze statically.  Clang static analyser is no exception.  Basically it analyzes functions independently (inlining some of the calls), and this model doesn’t allow it to reason about global variables.  It doesn’t know when <b class="">foo </b>is called because maybe some other function have already modified <b class="">x</b>.  Even if we put direct assignment <b class="">x = 0 </b>into <b class="">foo, </b>it is still hard to reason about.  Any call to another function is a potential modification of <b class="">x</b>.<div class=""><br class=""></div><div class="">I hope that answers you question!</div><div class=""><br class=""></div><div class="">-Valeriy Savchenko</div><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 26 May 2020, at 16:11, Denis Petrov via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="margin-top: 0px; margin-bottom: 0px; caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none;" class="">Run <span style="background-color: rgb(190, 190, 190);" class=""><span style="background-color: rgb(190, 190, 190);" class=""><span style="background-color: rgb(190, 190, 190);" class=""><span style="background-color: rgb(190, 190, 190);" class=""><span style="background-color: rgb(190, 190, 190);" class=""><span style="background-color: rgb(190, 190, 190);" class="">clang --analyze</span></span></span></span></span></span><span class="Apple-converted-space"> </span>on these two code snippets:<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none;" class=""><strong class="">global ptr:</strong></div><blockquote style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none; margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><div class=""><span style="font-size: 16px; background-color: rgb(255, 255, 255); font-family: Consolas, monospace;" class=""><span style="font-family: Consolas, monospace;" class="">int *x = 0;</span></span></div><div class=""><span style="font-size: 12pt; font-family: Consolas, monospace;" class=""><span style="font-family: Consolas, monospace;" class="">void foo() {</span></span></div><div class=""><span style="font-family: Consolas, monospace;" class="">  int y = *x;</span></div><div class=""><span style="font-family: Consolas, monospace;" class="">}</span></div></blockquote><div style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none;" class=""><strong class="">local </strong><strong style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; background-color: rgb(255, 255, 255);" class="">ptr:</strong></div><blockquote style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none; margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><div class=""><div style="font-size: 16px;" class=""><span style="font-family: Consolas, monospace;" class="">void foo() {</span></div></div><div class=""><div style="font-size: 16px;" class=""><span style="font-family: Consolas, monospace;" class="">  int *x = 0;</span></div></div><div class=""><div style="font-size: 16px;" class=""><span style="font-family: Consolas, monospace;" class="">  int y = *x;</span></div></div><div class=""><div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px;" class=""><span style="font-family: Consolas, monospace;" class="">}</span></div></div></blockquote><div style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none;" class=""><div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px;" class=""><span style="font-family: Consolas, monospace;" class=""><span style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; background-color: rgb(255, 255, 255);" class="">I met a </span><span style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; background-color: rgb(255, 255, 255);" class="">weird result.</span><span style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; background-color: rgb(255, 255, 255);" class="">​ </span></span></div><div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px;" class=""><span style="font-family: Consolas, monospace;" class=""><span style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; background-color: rgb(255, 255, 255);" class=""></span></span>The <strong class="">global<span class="Apple-converted-space"> </span></strong>version does not generate a <span style="text-decoration: underline;" class="">warning</span> like the<span class="Apple-converted-space"> </span><strong class="">local<span class="Apple-converted-space"> </span></strong>one does?<br class=""></div></div><blockquote style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none; margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><div class=""><div style="font-size: 16px;" class=""><span style="font-family: Consolas, monospace; background-color: rgb(190, 190, 190);" class=""><span style="background-color: rgb(190, 190, 190);" class="">test.cpp:3:11: warning: Dereference of null pointer (loaded from variable 'x') [core.NullDereference]</span></span></div></div></blockquote><span style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none;" class=""><span style="background-color: rgb(255, 255, 255); font-family: Calibri, Arial, Helvetica, sans-serif;" class=""><span style="font-family: Calibri, Arial, Helvetica, sans-serif;" class="">​Another observation is that for the global verion analyzer does not handle init expression <em class="">(int *x = 0;)</em>, but for the local one it does. Therefore it stores <strong class="">x</strong><span class="Apple-converted-space"> </span>as <strong class="">&SymRegion{reg_$0<int * x>}</strong><span class="Apple-converted-space"> </span>for the global and<span class="Apple-converted-space"> </span><strong class="">0</strong><span class="Apple-converted-space"> </span>for the local.</span></span></span><span style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none; float: none; display: inline !important;" class=""></span><div style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none;" class=""><br class=""></div><div style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none;" class=""><span style="background-color: rgb(255, 255, 255); font-family: Calibri, Arial, Helvetica, sans-serif;" class=""><span style="background-color: rgb(255, 255, 255); font-family: Calibri, Arial, Helvetica, sans-serif;" class=""><span style="font-family: Calibri, Arial, Helvetica, sans-serif;" class=""></span></span></span>Example graphs attached.<div class=""><div style="font-size: 16px;" class=""><span style="font-family: Calibri, Arial, Helvetica, sans-serif;" class=""><span style="font-family: Calibri, Arial, Helvetica, sans-serif;" class=""><br class=""></span></span></div><div style="font-size: 16px;" class=""><span style="font-family: Calibri, Arial, Helvetica, sans-serif;" class=""><span style="font-family: Calibri, Arial, Helvetica, sans-serif;" class="">Who can explain why it is so?</span></span></div></div><div style="margin-top: 0px; margin-bottom: 0px;" class="">​<br class=""></div><div id="Signature" class=""><div name="divtagdefaultwrapper" style="font-family: Calibri, Arial, Helvetica, sans-serif; margin: 0px;" class=""><hr class=""><div class=""><b class="">Denys Petrov</b></div><div class="">Senior С++ Developer | Kharkiv, Ukraine</div><div class=""><br class=""></div><div class=""></div></div></div></div><span id="cid:780D7769-295E-4709-AEEE-A310212ED95D@apple.com"><local_nullptr.html></span><span id="cid:3D6D624E-C8FA-49D7-9113-28204A98D0BD@apple.com"><global_nullptr.html></span><span style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none; float: none; display: inline !important;" class="">_______________________________________________</span><br style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none; float: none; display: inline !important;" class="">cfe-dev mailing list</span><br style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none;" class=""><a href="mailto:cfe-dev@lists.llvm.org" style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);" class="">cfe-dev@lists.llvm.org</a><br style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none;" class=""><a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);" class="">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br style="caret-color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none;" class=""></div></blockquote></div><br class=""></div></body></html>