<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=http://email.email.llvm.org/c/eJztWVtv47YS_jXOCxHBlmI7efCDL0k3p94kiLMtznkRKIq22ZVIlaQSe399ZyjJusROst0CBXpqCLZEDofDuXwzI0cq3k8-CY_c9vxxSjZKyA2xCi8ax8SolBO25ewr14YICcMrS61gU0mT_Teue_6cRLklL1suSx5MxcjklqSc24JDLvku48zymCRqI5jX6y96_WnY86dwfVIvRBhit5xkWkUJL-UoZsOC9kZoY4EpSkWJ5C-VWKTnXy5xI7onn_fz3FiVzospj2VZz7_CwzhioiSJhKR6T1TGNbVKm1KUpy1IUHE0Is2SPYnzNHNSVcSEyrgSc6NpSgzoghNqYRC-MhDalvx6o355FY9-ICRL8piDuD5LKJ7upqvKm1JuA7ezXCRWyHLkkW-EsSCEUNLbAovvY6s0dyw3jzxT2rpBeHraZ_xPc6t0_IPrP1NJNz_A5oHa7YpLI6x4brCdK2n5zjbZuu_coG9KmnKTUQa-7dgHs-OzHBz-MFl815O9cTkBPIzp-l4xReDTC6Yky6NEMFLNBXPnajAD14PmK5taGJw537wvva0XXMNVb1PwwDUl62cl4sJnKxb-JVMSwqTNCNQ4nWGgtnUDw6M5hodbUx9zvGjeA9UNXLVSmspwEnQP7g71HWLVmnrz85bwtZIIeShiE_2FP_I1YbnWYEf3DLZYkLm34cUjCAYsDqclJEme00J-rjEKcRrWzOFCXywA6S-4hnOJ7OqNZ-dgasSbVyI1xT9J9J7cnf3QxF0rFq6OLLTDGojMjl39y3bIognSjXZb1eqHEa_iULv7Kx-57pyhI1KkVNIQyWxVnsSPJwUrnOAj4mlucy2J1Tk_sncbtG8lqdAnEdE7eD3_TL_yJchnPLuzR_l5XpkcIIeFjnMIfDWEBB4Bn9sbHLi3V2MwHEt0bSpUbmfk5OlKuH3vhOWtZ-O3jxdD1LXEe6DsK5gEgfBwC8HrHyVCP73ugG4C-f2BYhyUVEUgH1kONUrT3ChKR1cOj2vPhP1e-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5cMh66Jpqfpo2FAXKaCLsPBeTt04R7aFUFC-0-e4tdScWoeUPA4hA5s-AFoYp-4-wt4iLnh9AnG0j6LcI3cXl2eH_myjsFaJgCLpHccEirpsBHfCcHWPlu1my1Ce_lllN55a418pESpcR7CRk-VgfIRon_PtgejsZBtI7p3wDb1db_17DtlMDH0b-w_S9s_wNh-1fsuMtqF5qahDAKiF024zBeFsS94Ibc64IQvih0chv8I6JoZm_O4kkQXwVX9Izmdqv0xMZyk--5vFuc5TqZbK3NDIK9ew27gdYvjzwo2as-q_g5h83wmPgyx5gcT3EzDAbj0dl2MhwGPO5HEeXDUexHPGKD_mV0cbEe8qurmNGzhEY8MRN0ed_Hf1YcC9TDcHH24xKIid8HPBgMgoE_HF_4Hh2NLgdRBPjAhjwYjXsXfZ5SkXjIx1N6c6YnjiVoysBk4t6qHSbB4bCR405gkNAKm7gH9zqj_H-KHN5hDRfkS_33U-ufmzMn6cSJ-Qf6abEQ>53176</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Clang Static Analyzer] Unexpected program state
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          tdnguyenND
      </td>
    </tr>
</table>

<pre>
    Hi. I'm going to to add some checkers into StaticAnalyzer, but when I'm coding I meet some unexpected logic.
_**How is the problem going**_
First I add a new checker (Let say MyCustomChecker.cpp) to check on binary operators.
This checker simply dump the operator and the program state at that point.
```
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"

using namespace clang;
using namespace ento;

namespace {
class MyCustomChecker
    : public Checker<check::PreStmt<BinaryOperator>> {
public:
  void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
};
} // namespace

void MyCustomChecker::checkPreStmt(const BinaryOperator *B,
                                 CheckerContext &C) const {
  ProgramStateRef currentState = C.getState();
  llvm::errs() << "************************\n";
  B->dump();
  currentState->dump();
  llvm::errs() << "\n";
}

void ento::registerMyCustomChecker(CheckerManager &mgr) {
  mgr.registerChecker<MyCustomChecker>();
}

bool ento::shouldRegisterMyCustomChecker(const CheckerManager &mgr) {
  return true;
}
```
In clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
```
...
add_clang_library(clangStaticAnalyzerCheckers
...
    MyCustomChecker.cpp
...
)
...
```
In clang/include/StaticAnalyzer/Checkers/Checkers.td
```
...
def CustomCheckerPackage: Package<"CustomCheckerPackage">;

let ParentPackage = CustomCheckerPackage in {
  def MyCustomChecker : Checker<"MyCustomChecker">,
    HelpText<"My first custom checker.">,
    Documentation<HasDocumentation>;
}
...
```
And add analyzer options for that package in clang/lib/Driver/ToolChains/Clang.cpp:
```
...
static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs,
                                  const llvm::Triple &Triple,
                                  const InputInfo &Input) {
    ...
  CmdArgs.push_back("-analyzer-checker=CustomCheckerPackage");
    ...
}
...
```
Then I build llvm with command
```
cmake -DLLVM_ENABLE_PROJECTS=clang -DLLVM_LINK_LLVM_DYLIB=true -DLLVM_ENABLE_Z3_SOLVER=true -G "Unix Makefiles" ../llvm
```
Now create a test file:
```
#include<stdlib.h>
#include<stdio.h>
void foo(int N, unsigned int x){
    int *ptr;
    ptr = malloc(N * sizeof(*ptr));
    if (ptr != NULL) {
        if(x < N){
            x = 2;
        }
    }
}
```
And run the scan-build command
`scan-build -constraints z3 clang -c test.c`
The problem is the state (which is dumped when checking the line `x = 2;` does not contain constraint `x < N`.
```
"program_state": {
  "store": { "pointer": "0x5637ec7ccb28", "items": [
    { "cluster": "GlobalSystemSpaceRegion", "pointer": "0x5637ec7cca50", "items": [
      { "kind": "Default", "offset": 0, "value": "conj_$1{int, LC1, S26714, #1}" }
    ]}
  ]},
  "environment": { "pointer": "0x5637ec66dcd0", "items": [
    { "lctx_id": 1, "location_context": "#0 Call", "calling": "foo", "location": null, "items": [
      { "stmt_id": 26777, "pretty": "x", "value": "&x" },
      { "stmt_id": 26785, "pretty": "2", "value": "2 U32b" }
    ]}
  ]},
  "constraints": null,
  "equivalence_classes": null,
  "disequality_info": null,
  "dynamic_types": null,
  "dynamic_casts": null,
  "constructing_objects": null,
  "checker_messages": null
}
```
But when I add one more useless line to the test file:
```
...
    if(x < N){
        x = 2;
        N;
    }
...
```
The state now do contain the constraint `x < N`.
```
"program_state": {
  "store": { "pointer": "0x55673bfdab28", "items": [
    { "cluster": "GlobalSystemSpaceRegion", "pointer": "0x55673bfdaa50", "items": [
      { "kind": "Default", "offset": 0, "value": "conj_$1{int, LC1, S26714, #1}" }
    ]}
  ]},
  "environment": { "pointer": "0x55673be7bcd0", "items": [
    { "lctx_id": 1, "location_context": "#0 Call", "calling": "foo", "location": null, "items": [
      { "stmt_id": 26777, "pretty": "x", "value": "&x" },
      { "stmt_id": 26785, "pretty": "2", "value": "2 U32b" }
    ]}
  ]},
  "constraints": null,
  "equivalence_classes": null,
  "disequality_info": null,
  "dynamic_types": null,
  "dynamic_casts": null,
  "constructing_objects": null,
  "checker_messages": null
}
```
What is the real cause for this problem? Or is this a bug on llvm?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztGV1z4jjy15AXVVxgB5M88MBHspM7JkmFzF7tvlCyLEA7tuST5ATm11-3bOOPQJLZ3aqp2hvKBba61d3q7zaRivfjT8Ijtz1_lJKNEnJDrMKLxjExKuWEbTn7yrUhQsLy0lIr2ETSZP-N654_I1FuycuWy5IGUzESuSUp57agkEu-yzizPCaJ2gjm9frzXn-y6vkTuD6pFyIMsVtOMq2ihJdyFNBVgXsjtLFAFKWiRPKXSizS8y8XyIjuyef9LDdWpbMC5LEs6_lXeBiHTJQkkZBU74nKuKZWaVOK8rQFC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