Skip to content

slow static analyzer performance with if-else chains #112970

@firewave

Description

@firewave
#include <string>

std::string replaceEscapeSequences(const std::string &source)
{
    std::string result;
    for (std::size_t i = 0; i < source.size(); ++i) {
        if (source[i] != '\\' || i + 1 >= source.size()) {
            result += source[i];
        }
        else {
            ++i;
            if (source[i] == 't') {
                result += '\t';
            } else if (source[i] == 'x')
            {
                std::string value = "0";
                if (i + 1 < source.size() && std::isxdigit(source[i+1])) {
                    value += source[i++ + 1];
                }
                if (i + 1 < source.size() && std::isxdigit(source[i+1])) {
                    value += source[i++ + 1];
                }
                result += static_cast<char>(std::stoi(value, nullptr, 16));
            }
        }
    }
    return result;
}
$ time clang-tidy-20 -checks='-*,clang-analyzer-*' utils.cpp
1 warning generated.
Suppressed 1 warnings (1 with check filters).

real    0m4.272s
user    0m3.984s
sys     0m0.266s

This has been reduced from https://github.com/danmar/cppcheck/blob/main/lib/utils.cpp. The total time of the whole file is about 10 seconds and the code above accounts for about half of that.

Any further reduction cuts the analysis time by a third or in half.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions