<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/66990>66990</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [analyzer] CStringChecker strlen should trust global constant initializers
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            good first issue,
            clang:static analyzer
      </td>
    </tr>

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

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

<pre>
    For constant global variables, we trust their initializers (if present):
```C++
const char *p = "abc";
const char s[] = "woof";
```
The CStringChecker when models "strlen" calls, it handles them by the `evalstrLengthCommon` function, which ensures preconditions of the call and acquires and binds the result: `strLength`.
That value is provided by the `getCStringLength` call, which should under the hood calculate the real length if possible.
Currently, it calculates it for string literals (aka. `strlen("foof")`), but not if the argument is a variable: `strlen(s)`, or `strlen(p)`.
This handler switch looks similar to how extents are calculated in MemRegion.cpp:getStaticSize.

Anyways, in general, if the const VarRegion is in the `GlobalImmutableSpaceRegion` memory space and has initializer, and the initializer expression there is "simple", we should just use that instead of the variable.
By "simple" I mean, if it's a StringLiteral after stipping implicit paren casts, I guess.

https://godbolt.org/z/v9vT75n4W
```C++
void clang_analyzer_dump(int);
typedef typeof(sizeof(int)) size_t;
size_t strlen(const char *);

const char text[] = "asdasdasd";
const char *p = "asdasdasd";
const char *q = p + 2;

void top(void) {
 clang_analyzer_dump(strlen("asdasdasd")); // should be 9
 clang_analyzer_dump(strlen(text)); // should be 9
 clang_analyzer_dump(strlen(p)); // should be 9
 clang_analyzer_dump(strlen(q)); // ideally, it should be 7, but it's fine if we don't know.
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVk2P4jgQ_TXmUhoUDARy4ECDWI20e9ke7R5HTlJJPO3YGbsCQ__6VTkJDd2a3ZVGighxyvXx6tWLVQi6tog7sX4S6-NM9dQ4vwuE6qVRZpa78ro7OQ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