github / codeql-coding-standards

This repository contains CodeQL queries and libraries which support various Coding Standards.
MIT License
129 stars 59 forks source link

`DeadCode`: Only consider a line dead if it is dead in every compilation #715

Closed lcartey closed 1 month ago

lcartey commented 1 month ago

Affected rules

Description

When intercepting a build we may see the same file be compiled multiple times in different contexts. When this happens, CodeQL effectively creates an internal "copy" of the analyzed code, specialized for each context. For dead code queries, we should only flag a line of code as dead if it is dead in all such instances.

Example

header_debug.h

void log(const char *a) {
  printf("%s", a);
}

header.h

void log(const char *a) {}

file.cpp

#if DEBUG
  #include 'header_debug.h
#else
  #include 'header.h
#endif

int main() {
  log("Hello world");
}