-
Notifications
You must be signed in to change notification settings - Fork 67
Implement generics package #867
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
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
64b7684
Implement generics package
MichaelRFairhurst cec1948
Use clang-format v11, which is closer to v14 installed on ci/cd
MichaelRFairhurst b2aef27
Format Type.qll
MichaelRFairhurst 183100e
Fix Generics.json package
MichaelRFairhurst 61b2852
Fix DeduplicateMacroResults test
MichaelRFairhurst 3074878
Fix query formats
MichaelRFairhurst aa01802
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/imp…
MichaelRFairhurst f1ec355
Fix query metadata
MichaelRFairhurst f363371
Update generic test expectation offsets post formatting
MichaelRFairhurst 2265ed4
Address feedback
MichaelRFairhurst bc00de6
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/imp…
MichaelRFairhurst 952253d
Format SimpleAssignment.qll
MichaelRFairhurst f268604
Update regex
MichaelRFairhurst 7831841
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/imp…
MichaelRFairhurst 6245f72
Format
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
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
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,155 @@ | ||
import cpp | ||
import codingstandards.cpp.Macro | ||
import codingstandards.cpp.MatchingParenthesis | ||
|
||
string genericRegexp() { result = ".*_Generic\\s*\\(\\s*(.+),.*" } | ||
|
||
bindingset[input] | ||
string deparenthesize(string input) { | ||
input = "(" + result + ")" and | ||
result = input.substring(1, input.length() - 1) | ||
} | ||
|
||
|
||
class GenericMacro extends Macro { | ||
string ctrlExpr; | ||
|
||
GenericMacro() { ctrlExpr = getBody().regexpCapture(genericRegexp(), 1).trim() } | ||
|
||
string getAParameter() { result = this.(FunctionLikeMacro).getAParameter() } | ||
|
||
string getControllingExprString() { | ||
if exists(string s | s = deparenthesize(ctrlExpr)) | ||
then result = deparenthesize(ctrlExpr).trim() | ||
else result = ctrlExpr | ||
} | ||
|
||
/** | ||
* Whether the controlling expression of the `_Generic` expr in this macro's controlling | ||
* expression refers to one of this macro's parameters. | ||
*/ | ||
predicate hasControllingExprFromMacroParameter() { | ||
getControllingExprString().matches(getAParameter()) | ||
} | ||
} | ||
|
||
class GenericMacroString extends string { | ||
GenericMacroString() { this = any(Macro m).getBody() and this.matches("%_Generic%") } | ||
} | ||
|
||
import MatchingParenthesis<GenericMacroString> | ||
|
||
class ParsedGenericMacro extends Macro { | ||
ParsedRoot macroBody; | ||
Parsed genericBody; | ||
string beforeGenericBody; | ||
string afterGenericBody; | ||
|
||
ParsedGenericMacro() { | ||
macroBody.getInputString() = this.getBody() and | ||
exists(ParsedText genericText | | ||
genericText.getText().matches("%_Generic%") and | ||
genericBody = genericText.getParent().getChild(genericText.getChildIdx() + 1) and | ||
genericBody.getRoot() = macroBody | ||
) and | ||
beforeGenericBody = | ||
textFrom(macroBody.getStartToken(), genericBody.getStartToken().getPrevious()) and | ||
( | ||
if exists(genericBody.getEndToken().getNext()) | ||
then afterGenericBody = textFrom(genericBody.getEndToken().getNext(), macroBody.getEndToken()) | ||
else afterGenericBody = "" | ||
) | ||
} | ||
|
||
string getAParameter() { | ||
result = this.(FunctionLikeMacro).getAParameter() | ||
} | ||
|
||
int getAParsedGenericCommaSeparatorOffset() { | ||
exists(ParsedText text | | ||
text.getParent() = genericBody and | ||
result = text.getStartToken().getStartPos() + text.getText().indexOf(",") | ||
) | ||
} | ||
|
||
int getAParsedGenericColonSeparatorOffset() { | ||
exists(ParsedText text | | ||
text.getParent() = genericBody and | ||
result = text.getStartToken().getStartPos() + text.getText().indexOf(":") | ||
) | ||
} | ||
|
||
int getParsedGenericCommaSeparatorOffset(int i) { | ||
result = rank[i](int index | index = getAParsedGenericCommaSeparatorOffset()) | ||
} | ||
|
||
bindingset[start, end] | ||
int getParsedGenericColon(int start, int end) { | ||
result = | ||
min(int offset | | ||
offset = getAParsedGenericColonSeparatorOffset() and | ||
offset >= start and | ||
offset <= end | ||
) | ||
} | ||
|
||
predicate hasParsedFullSelectionRange(int idx, int start, int end) { | ||
idx = 1 and | ||
start = genericBody.getStartToken().getEndPos() and | ||
end = getParsedGenericCommaSeparatorOffset(idx) | ||
or | ||
not exists(getParsedGenericCommaSeparatorOffset(idx)) and | ||
start = getParsedGenericCommaSeparatorOffset(idx - 1) and | ||
end = genericBody.getEndToken().getStartPos() | ||
or | ||
start = getParsedGenericCommaSeparatorOffset(idx - 1) and | ||
end = getParsedGenericCommaSeparatorOffset(idx) | ||
} | ||
|
||
string getSelectionString(int idx) { | ||
exists(int start, int rawStart, int end | | ||
hasParsedFullSelectionRange(idx, rawStart, end) and | ||
( | ||
if exists(getParsedGenericColon(rawStart, end)) | ||
then start = getParsedGenericColon(rawStart, end) | ||
else start = rawStart | ||
) and | ||
result = genericBody.getInputString().substring(start, end) | ||
) | ||
} | ||
|
||
string getControllingExprString() { | ||
result = getSelectionString(1) | ||
} | ||
|
||
bindingset[str, word] | ||
private int countWordInString(string word, string str) { | ||
result = | ||
max(int occurrence | | ||
exists(str.regexpFind("\\b" + word + "\\b", occurrence, _)) or occurrence = -1 | ||
| | ||
occurrence + 1 | ||
) | ||
} | ||
|
||
int expansionsOutsideExpr(string parameter) { | ||
parameter = getAParameter() and | ||
result = | ||
countWordInString(parameter, beforeGenericBody) + | ||
countWordInString(parameter, afterGenericBody) | ||
} | ||
|
||
int expansionsInsideSelection(string parameter, int idx) { | ||
parameter = getAParameter() and | ||
result = countWordInString(parameter, getSelectionString(idx)) | ||
} | ||
|
||
int expansionsInsideControllingExpr(string parameter) { | ||
result = expansionsInsideSelection(parameter, 1) | ||
} | ||
|
||
int expansionsInsideAssociation(string parameter, int idx) { | ||
not idx = 0 and | ||
result = expansionsInsideSelection(parameter, idx + 1) | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
26 changes: 26 additions & 0 deletions
26
c/misra/src/rules/RULE-23-1/GenericSelectionDoesntDependOnMacroArgument.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,26 @@ | ||
/** | ||
* @id c/misra/generic-selection-doesnt-depend-on-macro-argument | ||
* @name RULE-23-1: A generic selection should depend on the type of a macro argument | ||
* @description A generic selection should depend on the type of a macro argument. | ||
* @kind problem | ||
* @precision high | ||
* @problem.severity warning | ||
* @tags external/misra/id/rule-23-1 | ||
* correctness | ||
* maintainability | ||
* external/misra/c/2012/amendment3 | ||
* external/misra/obligation/advisory | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.c.misra | ||
import codingstandards.c.Generic | ||
|
||
from ParsedGenericMacro macro, string ctrlExpr | ||
where | ||
not isExcluded(macro, GenericsPackage::genericSelectionDoesntDependOnMacroArgumentQuery()) and | ||
ctrlExpr = macro.getControllingExprString().trim() and | ||
MichaelRFairhurst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
not macro.expansionsInsideControllingExpr(_) > 0 | ||
MichaelRFairhurst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
select macro, | ||
"Generic macro " + macro.getName() + " uses controlling expr " + ctrlExpr + | ||
MichaelRFairhurst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
", which doesn't match any macro parameter." |
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.