주요 콘텐츠

Detect Use of std Namespace in using Declaration

This example shows how to use Polyspace Query Language (PQL) to check for violation of this rule: Do not use using namespace std.

This topic focuses on application of PQL. For more details about creating syntax defect checkers, see Detect Syntactic Issues Using Polyspace Query Language Syntactic Classes.

Analyze Rule

To implement the rule in PQL, analyze the components of the rule.

  • using declaration — Use the class UsingDeclaration to identify using declarations.

  • Namespace identifier in using declaration — Use the predicates of the class UsingDeclaration to find the namespace identifier and check if it matches std.

Implement Rule in PQL

The rule can be implemented using the predicates of the class UsingDeclaration:

defect use_of_namespace_std =
        when
            Cpp.UsingDeclaration.is(&ns)
            and ns.identifier(&ns_id)
            and ns_id.nodeText(&ns_id_name)
            and ns_id_name=="std"
        raise "Explicitly prefix all identifiers from the standard namespace with an 'std::' prefix, rather than rely on 'using namespace std;'"
        on ns,

Test Defect

To test and verify the defect:

  1. Initialize a test standard in a writable location:

    polyspace-query-language init

  2. In main.pql, insert the defect in a test standard:

    package main
    
    // Main PQL file defines the catalog of your PQL project.
    // The catalog is a collection of sections.
    catalog PQL_Using_example = {
    	#[Description("MySection")]
        section mysection = {
            #[Description("myRule"), Id(Rule1)]
            rule myRule = {
                defect use_of_namespace_std =
            when
                Cpp.UsingDeclaration.is(&ns)
                and ns.identifier(&ns_id)
                and ns_id.nodeText(&ns_id_name)
                and ns_id_name=="std"
            raise "Explicitly prefix all identifiers from the standard namespace with an 'std::' prefix, rather than rely on 'using namespace std;'"
            on ns,
            }
        }
    }
    
    

  3. Package the standard in a pschk file:

    polyspace-query-language package

  4. Create example.cpp in the same folder:

    /* Do Not Use 'using namespace std'. */
    #include <iostream>
    
    namespace mathutils {
        int add(int a, int b) {
            return a + b;
        }
    }
    
    using namespace std;        // expect-1-Rule1
    using namespace mathutils;  // expect-0-Rule1
    
    int main() {
        cout << "3 + 4 = " << add(3, 4) << endl;
        return 0;
    }
    
    This code explicitly states where a violation of the rule is expected using annotation expect-1-Rule1. Absence of the violation is also annotated using expect-0-Rule1.

  5. Run a test for the defect on the example code:

    polyspace-query-language test example.cpp
    The test verifies the expected violations as well as the expected absence of violations:
    Number of actual defects: 1
    Number of expected annotations: 2 (including 1 expected absence of defects).
    _______________________________________________
      Checking expected defects with actuals...
      -----------------------------------------
    _______________________________________________
      Looking for unexpected defects...
    -------------------------------------------
    _______________________________________________
    Tests passed

See Also

Topics