- Notifications
You must be signed in to change notification settings - Fork70
AddTemplate query library#932
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
jeongsoolee09 wants to merge2 commits intomainChoose a base branch fromjeongsoolee09/add-template-lib
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
207 changes: 207 additions & 0 deletionscpp/common/src/codingstandards/cpp/Template.qll
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,207 @@ | ||
| import cpp | ||
| newtype TTemplateElement = | ||
| TTemplateClass(TemplateClass c) or | ||
| TTemplateFunction(TemplateFunction f) or | ||
| TTemplateVariable(TemplateVariable v) | ||
| /** | ||
| * A templated element. These are either templated classes, templated functions, | ||
| * or templated variables. | ||
| */ | ||
| class TemplateElement extends TTemplateElement { | ||
| TemplateClass asTemplateClass() { this = TTemplateClass(result) } | ||
| TemplateFunction asTemplateFunction() { this = TTemplateFunction(result) } | ||
| TemplateVariable asTemplateVariable() { this = TTemplateVariable(result) } | ||
| string toString() { | ||
| result = this.asTemplateClass().toString() or | ||
| result = this.asTemplateFunction().toString() or | ||
| result = this.asTemplateVariable().toString() | ||
| } | ||
| Location getLocation() { | ||
| result = this.asTemplateClass().getLocation() or | ||
| result = this.asTemplateFunction().getLocation() or | ||
| result = this.asTemplateVariable().getLocation() | ||
| } | ||
| string getName() { | ||
| result = this.asTemplateClass().getName() or | ||
| result = this.asTemplateFunction().getName() or | ||
| result = this.asTemplateVariable().getName() | ||
| } | ||
| } | ||
| newtype TTemplateInstantiation = | ||
| TClassTemplateInstantiation(ClassTemplateInstantiation c) or | ||
| TFunctionTemplateInstantiation(FunctionTemplateInstantiation f) or | ||
| TVariableTemplateInstantiation(VariableTemplateInstantiation v) | ||
| /** | ||
| * An instantiation of a templated element, either a templated class, templated | ||
| * function, or templated variable. | ||
| */ | ||
| class TemplateInstantiation extends TTemplateInstantiation { | ||
| ClassTemplateInstantiation asClassTemplateInstantiation() { | ||
| this = TClassTemplateInstantiation(result) | ||
| } | ||
| FunctionTemplateInstantiation asFunctionTemplateInstantiation() { | ||
| this = TFunctionTemplateInstantiation(result) | ||
| } | ||
| VariableTemplateInstantiation asVariableTemplateInstantiation() { | ||
| this = TVariableTemplateInstantiation(result) | ||
| } | ||
| string toString() { | ||
| result = this.asClassTemplateInstantiation().toString() or | ||
| result = this.asFunctionTemplateInstantiation().toString() or | ||
| result = this.asVariableTemplateInstantiation().toString() | ||
| } | ||
| Location getLocation() { | ||
| result = this.asClassTemplateInstantiation().getLocation() or | ||
| result = this.asFunctionTemplateInstantiation().getLocation() or | ||
| result = this.asVariableTemplateInstantiation().getLocation() | ||
| } | ||
| Element asElement() { | ||
| result = this.asClassTemplateInstantiation() or | ||
| result = this.asFunctionTemplateInstantiation() or | ||
| result = this.asVariableTemplateInstantiation() | ||
| } | ||
| /** | ||
| * Gets the template this instantiation is from, depending on the kind of the element | ||
| * this instantiation is for. | ||
| */ | ||
| TemplateElement getTemplate() { | ||
| result.asTemplateClass() = this.asClassTemplateInstantiation().getTemplate() or | ||
| result.asTemplateFunction() = this.asFunctionTemplateInstantiation().getTemplate() or | ||
| result.asTemplateVariable() = this.asVariableTemplateInstantiation().getTemplate() | ||
| } | ||
| /** | ||
| * Gets a use of an instantiation of this template. i.e. | ||
| * 1. For a class template, it's where the instantiated type is used by the name. | ||
| * 2. For a function template, it's where the instantiated function is called. | ||
| * 3. For a variable template, it's where the instantiated variable is initialized. | ||
| */ | ||
| Element getAUse() { | ||
| result = this.asClassTemplateInstantiation().getATypeNameUse() or | ||
| result = this.asFunctionTemplateInstantiation().getACallToThisFunction() or | ||
| result = this.asVariableTemplateInstantiation() | ||
| } | ||
| } | ||
| /** | ||
| * An implicit conversion from a plain char type to an explicitly signed or unsigned char | ||
| * type. `std::uint8_t` and `std::int8_t` are also considered as these char types. | ||
| * | ||
| * Note that this class only includes implicit conversions and does not include explicit | ||
| * type conversions, i.e. casts. | ||
| */ | ||
| class ImplicitConversionFromPlainCharType extends Conversion { | ||
| ImplicitConversionFromPlainCharType() { | ||
| this.isImplicit() and | ||
| this.getExpr().getUnspecifiedType() instanceof PlainCharType and | ||
| ( | ||
| this.getUnspecifiedType() instanceof SignedCharType or | ||
| this.getUnspecifiedType() instanceof UnsignedCharType | ||
| ) | ||
| } | ||
| } | ||
| newtype TImplicitConversionElement = | ||
| TImplicitConversionOutsideTemplate(ImplicitConversionFromPlainCharType implicitConversion) { | ||
| not exists(TemplateInstantiation instantiation | | ||
| implicitConversion.isFromTemplateInstantiation(instantiation.asElement()) | ||
| ) | ||
| } or | ||
| TInstantiationOfImplicitConversionTemplate( | ||
| TemplateInstantiation templateInstantiation, | ||
| ImplicitConversionFromPlainCharType implicitConversion | ||
| ) { | ||
| implicitConversion.getEnclosingElement+() = templateInstantiation.asElement() | ||
| } | ||
| /** | ||
| * The locations where the implicit conversion from a plain char to an explicitly signed / unsigned | ||
| * char is taking place on a high level. It splits case on whether the conversion is caused by | ||
| * instantiating a template: | ||
| * | ||
| * - For conversions not due to template usage (i.e. outside a templated element), this refers to | ||
| * the same element as the one associated with the conversion. | ||
| * - For conversions due to template usage, this refers to the element that uses the instantiation | ||
| * of a template where an implicit char conversion happens. | ||
| */ | ||
| class ImplicitConversionLocation extends TImplicitConversionElement { | ||
| ImplicitConversionFromPlainCharType asImplicitConversionOutsideTemplate() { | ||
| this = TImplicitConversionOutsideTemplate(result) | ||
| } | ||
| TemplateInstantiation asInstantiationOfImplicitConversionTemplate( | ||
| ImplicitConversionFromPlainCharType implicitConversion | ||
| ) { | ||
| this = TInstantiationOfImplicitConversionTemplate(result, implicitConversion) | ||
| } | ||
| /** | ||
| * Holds if this is a location of a conversion happening outside of a template. | ||
| */ | ||
| predicate isImplicitConversionOutsideTemplate() { | ||
| exists(this.asImplicitConversionOutsideTemplate()) | ||
| } | ||
| /** | ||
| * Holds if this is a location of a conversion happening due to instantiating a | ||
| * template. | ||
| */ | ||
| predicate isInstantiationOfImplicitConversionTemplate() { | ||
| exists( | ||
| TemplateInstantiation templateInstantiation, | ||
| ImplicitConversionFromPlainCharType implicitConversion | ||
| | | ||
| templateInstantiation = this.asInstantiationOfImplicitConversionTemplate(implicitConversion) | ||
| ) | ||
| } | ||
| /** | ||
| * Gets the implicit conversion that this location is associated with. | ||
| * - In cases of conversions not involving a template, this is the same as the | ||
| * location associated with the conversion. | ||
| * - In cases of conversions due to using a template, this is the conversion that | ||
| * happens in the instantiated template. | ||
| */ | ||
| ImplicitConversionFromPlainCharType getImplicitConversion() { | ||
| result = this.asImplicitConversionOutsideTemplate() or | ||
| exists(TemplateInstantiation templateInstantiation | | ||
| this = TInstantiationOfImplicitConversionTemplate(templateInstantiation, result) | ||
| ) | ||
| } | ||
| string toString() { | ||
| result = this.asImplicitConversionOutsideTemplate().toString() or | ||
| exists(ImplicitConversionFromPlainCharType implicitConversion | | ||
| result = this.asInstantiationOfImplicitConversionTemplate(implicitConversion).toString() | ||
| ) | ||
| } | ||
| Location getLocation() { | ||
| result = this.asImplicitConversionOutsideTemplate().getLocation() or | ||
| exists(ImplicitConversionFromPlainCharType implicitConversion | | ||
| result = this.asInstantiationOfImplicitConversionTemplate(implicitConversion).getLocation() | ||
| ) | ||
| } | ||
| Element asElement() { | ||
| result = this.asImplicitConversionOutsideTemplate() or | ||
| exists(ImplicitConversionFromPlainCharType implicitConversion | | ||
| result = this.asInstantiationOfImplicitConversionTemplate(implicitConversion).getAUse() | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.