C++: Fix missing bool -> int conversions in C code#20145
Merged
MathiasVP merged 3 commits intogithub:mainfrom Aug 11, 2025
Merged
C++: Fix missing bool -> int conversions in C code#20145MathiasVP merged 3 commits intogithub:mainfrom
bool -> int conversions in C code#20145MathiasVP merged 3 commits intogithub:mainfrom
Conversation
a0f296d to
6e209d0
Compare
6e209d0 to
14345a8
Compare
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes missing bool to int conversions in C code that were causing type errors in the IR. When comparisons and other boolean operations in C code store their results to integer variables, the IR previously showed incorrect type mismatches where boolean values were being stored to integer memory locations.
- Adds
TranslatedSyntheticBoolToIntConversionclass to handle bool-to-int conversions - Introduces
hasTranslatedSyntheticBoolToIntConversionpredicate to identify expressions needing conversion - Updates instruction tags and test expectations to reflect the new conversion instructions
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedExpr.qll | Adds TranslatedSyntheticBoolToIntConversion class and updates core expression handling |
| cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll | Adds predicate logic to identify expressions needing bool-to-int conversion |
| cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/InstructionTag.qll | Adds BoolToIntConversionTag for the new conversion instruction |
| cpp/ql/test/library-tests/ir/ir/ir.c | Adds test case for double negation assignment |
| cpp/ql/test/library-tests/ir/ir/raw_ir.expected | Updates expected IR output to include new conversion instructions |
| cpp/ql/test/library-tests/ir/ir/aliased_ir.expected | Updates expected aliased IR output with conversion instructions |
| cpp/ql/test/library-tests/ir/ir/PrintAST.expected | Updates expected AST output for new test case |
Comments suppressed due to low confidence (1)
cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll:530
- The predicate name
parentExpectsBoolis misleading. It should be named something likeparentExpectsBoolValueorusedInBooleanContextsince it checks if the child expression is used in a context where a boolean value is expected, not just any parent.
private predicate parentExpectsBool(Expr child) {
any(NotExpr notExpr).getOperand() = child
or
usedAsCondition(child)
}
jketema
approved these changes
Aug 11, 2025
This was referenced Aug 13, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In #18490 we fixed a number of discrepancies between IR generated from C code, and IR generated from C++ code. For example, the following code has a int-to-bool conversion in the AST on
xat the conditional when compiled as C++, but not when compiled as C:#18490 removed this discrepancy in the IR by synthesizing the equivalent int-to-bool conversion when generating IR. One of the things we did was adjust the
IRTypereturned by various operations (see here)However, when I was working on something else I noticed that this change introduced a type error in the IR on examples such as:
Because
a < bhas been overwritten to return a boolean, theStoreInstructioncorresponding to the initialization ofxlooks like:In particular, notice that we are storing a result of type
boolinto a memory address of typeglval<int>. That's not allowed in the IR, and it gives some false negatives in the guards library.This PR fixes that by adding the necessary bool-to-int conversions when such type errors would have occurred in the IR.
Commit-by-commit review highly encouraged.
DCA shows a couple new results for
cpp/missing-check-scanf. They look like FPs caused by insufficient guards logic (which we now need now that those conditionals are properly handled in the IR). I will take a look at once this PR has been merged.