`M14-6-1`: enhancement to cover types

Open
#221 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp
Domain
devtools

Research direction

Start with linked issue 192 and the M14-6-1 query draft, including NameInDependentBase, TypeUses, and TemplatesPackage. Inspect the typedef and type-use behavior shown in the C++ example, then determine whether the query can cover qualified and unqualified dependent-base typedef uses accurately and without the old library's performance problems.

Written by the indexing model from the issue text.

Description

Standard-AUTOSAR

as noted in this issue this rule does not address the types case

the reason was that at the time of writing this query it was discovered that we dont have fully qualified typenames for typedefs:

typedef int TYPE;

template<typename T>
class B {
    public:
    typedef T TYPE;
};

template<typename T>
class A : B<T> {
    public:
    void m2(){
        TYPE t = 0; // NON_COMPLIANT
        ::TYPE t1 = 0; //COMPLIANT
        typename B<T>::TYPE t2 = 0; // COMPLIANT
    }
};

void f(){
    A<int> a;
    a.m2();
}

example, trying to get full names of types present:

from TypedefType t
select t, t.getQualifiedName(), t.getATypeNameUse()

result:

+| test.cpp:1:13:1:16 | TYPE | TYPE | test.cpp:20:14:20:14 | definition of t |
+| test.cpp:1:13:1:16 | TYPE | TYPE | test.cpp:26:16:26:17 | definition of t1 |

and initial audit level query was written but it was imprecise and used the old lib (so also non performat). would need cleanup or rewriting

/**
 * @id cpp/autosar/name-not-referred-using-a-qualified-id-or-this-type-def-type
 * @name M14-6-1: In a class template with a dependent base, any typedef type name that may be found in that dependent base shall shall be referred to using a qualified-id or this->
 * @description Not using a qualified-id or `this->` syntax for typedef type uses in a class
 *              template makes the code more difficult to understand.
 * @kind problem
 * @precision very-high
 * @problem.severity warning
 * @tags external/autosar/id/m14-6-1
 *       maintainability
 *       readability
 *       external/autosar/allocated-target/implementation
 *       external/autosar/enforcement/automated
 *       external/autosar/obligation/required
 */

import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.TypeUses
import NameInDependentBase

/**
 * `Locatable`s exists only in some `MemberFunction`
 *  this class is required to speed up `TypeUses.getATypeUse`
 */
class LocatablesInChild extends Locatable {
  Class child;

  LocatablesInChild() { this.getEnclosingElement() = child.getAMemberFunction() }

  predicate isInThisChild(Class c) { c = child }
}

/**
 * `TypedefType`s that are declared in a `Class`
 *  that is a parent of any other `Class`
 */
class ParentTypedefType extends TypedefType {
  Class parent;
  Class child;

  ParentTypedefType() {
    this.getDeclaringType() = parent and
    getParent(child) = parent
  }

  predicate thisIsMyChild(Class c) { c = child }
}

/**
 * All pairs of `TypedefType` and `ParentTypedefType`
 * with a same name
 */
predicate sameNameTypeDefPairs(TypedefType v, ParentTypedefType parentType) {
  v.getName() = parentType.getName()
}

from Class c, TypedefType v, ParentTypedefType parentType, LocatablesInChild l
where
  not isExcluded(l, TemplatesPackage::nameNotReferredUsingAQualifiedIdOrThisTypeDefTypeQuery()) and
  l.isInThisChild(c) and
  parentType.thisIsMyChild(c) and
  l = getATypeUse(v) and
  //two TypedefTypes with same name but one was declared in the dependent base
  sameNameTypeDefPairs(v, parentType)
select l,
  "Use of typedef type name that also exists in a base class that may not be fully qualified."
Dominant language
CodeQL
Stars
227
Forks
82
Avg merge
6d 7h
Merged PRs (30d)
9

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from github/codeql-coding-standards

All issues in github/codeql-coding-standards

Similar issues

More DevTools issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.