Differential D97204 Diff 325762 clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp

# Changeset View

Changeset View

# Standalone View

Standalone View

# clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp

Show First 20 Lines • Show All 125 Lines • ▼ Show 20 Lines | struct CognitiveComplexity final { | ||||

// Limit of 25 is the "upstream"'s default. | // Limit of 25 is the "upstream"'s default. | ||||

static constexpr unsigned DefaultLimit = 25U; | static constexpr unsigned DefaultLimit = 25U; | ||||

// Based on the publicly-avaliable numbers for some big open-source projects | // Based on the publicly-avaliable numbers for some big open-source projects | ||||

// https://sonarcloud.io/projects?languages=c%2Ccpp&size=5 we can estimate: | // https://sonarcloud.io/projects?languages=c%2Ccpp&size=5 we can estimate: | ||||

// value ~20 would result in no allocs for 98% of functions, ~12 for 96%, ~10 | // value ~20 would result in no allocs for 98% of functions, ~12 for 96%, ~10 | ||||

// for 91%, ~8 for 88%, ~6 for 84%, ~4 for 77%, ~2 for 64%, and ~1 for 37%. | // for 91%, ~8 for 88%, ~6 for 84%, ~4 for 77%, ~2 for 64%, and ~1 for 37%. | ||||

static_assert(sizeof(Detail) <= 8, | static_assert(sizeof(Detail) <= 16, | ||||

"Since we use SmallVector to minimize the amount of " | "Since we use SmallVector to minimize the amount of " | ||||

"allocations, we also need to consider the price we pay for " | "allocations, we also need to consider the price we pay for " | ||||

"that in terms of stack usage. " | "that in terms of stack usage. " | ||||

"Thus, it is good to minimize the size of the Detail struct."); | "Thus, it is good to minimize the size of the Detail struct."); | ||||

SmallVector<Detail, DefaultLimit> Details; // 25 elements is 200 bytes. | SmallVector<Detail, DefaultLimit> Details; // 25 elements is 400 bytes. | ||||

// Yes, 25 is a magic number. This is the seemingly-sane default for the | // Yes, 25 is a magic number. This is the seemingly-sane default for the | ||||

// upper limit for function cognitive complexity. Thus it would make sense | // upper limit for function cognitive complexity. Thus it would make sense | ||||

// to avoid allocations for any function that does not violate the limit. | // to avoid allocations for any function that does not violate the limit. | ||||

// The grand total Cognitive Complexity of the function. | // The grand total Cognitive Complexity of the function. | ||||

unsigned Total = 0; | unsigned Total = 0; | ||||

// The function used to store new increment, calculate the total complexity. | // The function used to store new increment, calculate the total complexity. | ||||

▲ Show 20 Lines • Show All 408 Lines • Show Last 20 Lines |