-
Notifications
You must be signed in to change notification settings - Fork 66
Implement EssentialTypes2 package #871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MichaelRFairhurst
merged 5 commits into
main
from
michaelrfairhurst/implement-essential-types2-package
Mar 31, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
076b1c4
Implement EssentialTypes2 package
MichaelRFairhurst 6148518
Format qll, update package metadata
MichaelRFairhurst 642c737
Handle bit fields
MichaelRFairhurst 94bad29
Format
MichaelRFairhurst ae557d9
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/imp…
MichaelRFairhurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import cpp | ||
|
||
private string getATgMathMacroName(boolean allowComplex) { | ||
allowComplex = true and | ||
result = | ||
[ | ||
"acos", "acosh", "asin", "asinh", "atan", "atanh", "carg", "cimag", "conj", "cos", "cosh", | ||
"cproj", "creal", "exp", "fabs", "log", "pow", "sin", "sinh", "sqrt", "tan", "tanh" | ||
] | ||
or | ||
allowComplex = false and | ||
result = | ||
[ | ||
"atan2", "cbrt", "ceil", "copysign", "erf", "erfc", "exp2", "expm1", "fdim", "floor", "fma", | ||
"fmax", "fmin", "fmod", "frexp", "hypot", "ilogb", "ldexp", "lgamma", "llrint", "llround", | ||
"log10", "log1p", "log2", "logb", "lrint", "lround", "nearbyint", "nextafter", "nexttoward", | ||
"remainder", "remquo", "rint", "round", "scalbn", "scalbln", "tgamma", "trunc", | ||
] | ||
} | ||
|
||
private predicate hasOutputArgument(string macroName, int index) { | ||
macroName = "frexp" and index = 1 | ||
or | ||
macroName = "remquo" and index = 2 | ||
} | ||
|
||
class TgMathInvocation extends MacroInvocation { | ||
Call call; | ||
boolean allowComplex; | ||
|
||
TgMathInvocation() { | ||
this.getMacro().getName() = getATgMathMacroName(allowComplex) and | ||
call = getBestCallInExpansion(this) | ||
} | ||
|
||
Expr getOperandArgument(int i) { | ||
result = call.getArgument(i) and | ||
not hasOutputArgument(call.getTarget().getName(), i) | ||
} | ||
|
||
int getNumberOfOperandArguments() { | ||
result = call.getNumberOfArguments() - count(int i | hasOutputArgument(getMacroName(), i)) | ||
} | ||
|
||
Expr getAnOperandArgument() { result = getOperandArgument(_) } | ||
|
||
predicate allowsComplex() { allowComplex = true } | ||
} | ||
|
||
private Call getACallInExpansion(MacroInvocation mi) { result = mi.getAnExpandedElement() } | ||
|
||
private Call getNameMatchedCallInExpansion(MacroInvocation mi) { | ||
result = getACallInExpansion(mi) and result.getTarget().getName() = mi.getMacroName() | ||
} | ||
|
||
private Call getBestCallInExpansion(MacroInvocation mi) { | ||
count(getACallInExpansion(mi)) = 1 and result = getACallInExpansion(mi) | ||
or | ||
count(getNameMatchedCallInExpansion(mi)) = 1 and result = getNameMatchedCallInExpansion(mi) | ||
or | ||
count(getNameMatchedCallInExpansion(mi)) > 1 and | ||
result = rank[1](Call c | c = getACallInExpansion(mi) | c order by c.getTarget().getName()) | ||
} |
47 changes: 47 additions & 0 deletions
47
c/misra/src/rules/RULE-21-22/TgMathArgumentWithInvalidEssentialType.ql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @id c/misra/tg-math-argument-with-invalid-essential-type | ||
* @name RULE-21-22: All operand arguments to type-generic macros in <tgmath.h> shall have an appropriate essential type | ||
* @description All operand arguments to any type-generic macros in <tgmath.h> shall have an | ||
* appropriate essential type. | ||
* @kind problem | ||
* @precision high | ||
* @problem.severity error | ||
* @tags external/misra/id/rule-21-22 | ||
* correctness | ||
* external/misra/c/2012/amendment3 | ||
* external/misra/obligation/mandatory | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.c.misra | ||
import codingstandards.c.TgMath | ||
import codingstandards.c.misra.EssentialTypes | ||
|
||
EssentialTypeCategory getAnAllowedEssentialTypeCategory(TgMathInvocation call) { | ||
result = EssentiallySignedType() | ||
or | ||
result = EssentiallyUnsignedType() | ||
or | ||
result = EssentiallyFloatingType(Real()) | ||
or | ||
call.allowsComplex() and | ||
result = EssentiallyFloatingType(Complex()) | ||
} | ||
|
||
string getAllowedTypesString(TgMathInvocation call) { | ||
if call.allowsComplex() | ||
then result = "essentially signed, unsigned, or floating type" | ||
else result = "essentially signed, unsigned, or real floating type" | ||
} | ||
|
||
from TgMathInvocation call, Expr arg, int argIndex, Type type, EssentialTypeCategory category | ||
where | ||
not isExcluded(call, EssentialTypes2Package::tgMathArgumentWithInvalidEssentialTypeQuery()) and | ||
arg = call.getOperandArgument(argIndex) and | ||
type = getEssentialType(arg) and | ||
category = getEssentialTypeCategory(type) and | ||
not category = getAnAllowedEssentialTypeCategory(call) | ||
select arg, | ||
"Argument " + (argIndex + 1) + " provided to type-generic macro '" + call.getMacroName() + | ||
"' has " + category.toString().toLowerCase() + ", which is not " + getAllowedTypesString(call) + | ||
"." |
75 changes: 75 additions & 0 deletions
75
c/misra/src/rules/RULE-21-23/TgMathArgumentsWithDifferingStandardType.ql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* @id c/misra/tg-math-arguments-with-differing-standard-type | ||
* @name RULE-21-23: Operand arguments for an invocation of a type-generic macro shall have the same standard type | ||
* @description All operand arguments to any multi-argument type-generic macros in <tgmath.h> shall | ||
* have the same standard type. | ||
* @kind problem | ||
* @precision high | ||
* @problem.severity error | ||
* @tags external/misra/id/rule-21-23 | ||
* correctness | ||
* external/misra/c/2012/amendment3 | ||
* external/misra/obligation/required | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.c.misra | ||
import codingstandards.c.TgMath | ||
|
||
string argTypesString(TgMathInvocation call, int i) { | ||
exists(string typeStr | | ||
typeStr = getEffectiveStandardType(call.getOperandArgument(i)).toString() and | ||
( | ||
i = 0 and result = typeStr | ||
or | ||
i > 0 and result = argTypesString(call, i - 1) + ", " + typeStr | ||
) | ||
) | ||
} | ||
|
||
/** | ||
* If the range of values can be represented as a signed int, it is promoted to signed int. | ||
* | ||
* A value may also promote to unsigned int but only if `int` cannot represent the range of | ||
* values. Which basically means only an `unsigned int` promotes to `unsigned int`, so we don't | ||
* need to do anything in this case. | ||
* | ||
* An unsigned int bitfield with fewer than 32 bits is promoted to `int`. | ||
*/ | ||
predicate promotesToSignedInt(Expr e) { | ||
exists(int intBits, int intBytes | | ||
intBytes = any(IntType t).getSize() and | ||
intBits = intBytes * 8 and | ||
( | ||
e.(FieldAccess).getTarget().(BitField).getNumBits() < intBits | ||
or | ||
e.getUnderlyingType().(IntegralType).getSize() < intBytes | ||
) | ||
) | ||
} | ||
|
||
Type getPromotedType(Expr e) { | ||
if promotesToSignedInt(e) then result.(IntType).isSigned() else result = e.getUnderlyingType() | ||
} | ||
|
||
Type canonicalize(Type type) { | ||
if type instanceof IntegralType | ||
then result = type.(IntegralType).getCanonicalArithmeticType() | ||
else result = type | ||
} | ||
|
||
Type getEffectiveStandardType(Expr e) { | ||
result = canonicalize(getPromotedType(e.getExplicitlyConverted())) | ||
} | ||
|
||
from TgMathInvocation call, Type firstType | ||
where | ||
not isExcluded(call, EssentialTypes2Package::tgMathArgumentsWithDifferingStandardTypeQuery()) and | ||
firstType = getEffectiveStandardType(call.getAnOperandArgument()) and | ||
not forall(Expr arg | arg = call.getAnOperandArgument() | | ||
firstType = getEffectiveStandardType(arg) | ||
) | ||
select call, | ||
"Call to type-generic macro '" + call.getMacroName() + | ||
"' has arguments with differing standard types (" + | ||
argTypesString(call, call.getNumberOfOperandArguments() - 1) + ")." |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.