Creating Custom Coding Rule for Comments

I am looking to create a custom ruleset to check whether code contains certain comments (block comments/normal comments) during static analysis.

Any assistance on this regard would be highly appreciated

Comments

  • random_anonymousrandom_anonymous Posts: 7
    edited March 7

    I can't seem to find any parameters for comments or such on "choose body", while creating a new ruleset.

  • mstaronmstaron Posts: 7

    You need to create C++Text rule in RuleWizard:
    Choose File>New>Rule from the menu bar.
    Choose C++Text from the Dictionary drop-down menu and enable the By Node option.
    Choose Comments (// or /*) and click OK.

  • mstaronmstaron Posts: 7

    The body of the comment is represented as a string and is accessible by the property 'Name'.
    Right-click the Comments node and choose Name(S) from the shortcut menu.

    Regular expressions are used to match strings.
    See https://docs.parasoft.com/display/RW/Expressions+and+Regular+Expressions#ExpressionsandRegularExpressions-RegularExpressions

  • random_anonymousrandom_anonymous Posts: 7

    Is there a way to check whether the last line of a file contains a certain comment?

  • mstaronmstaron Posts: 7

    If it is the last line of a file, then should be created rule like this:

    If after comment line is an empty line, then should be created rule like this:

  • JeffEstesJeffEstes Posts: 47 admin

    Rule Wizard also has Python scripting capability for rules. If the requirements for header/footer content gets too complicated, doing it with Rule Wizard "blocks" may get too intense. Look at the RuleWizardUsersGuide for information on this Eg) "Using a Python Method to Enforce a File Naming Rule"

  • random_anonymousrandom_anonymous Posts: 7

    @mstaron I tried your first method, this doesn't seem to work. My block diagram is exactly the same as yours. I am unable to detect for a match/false match in any source code. Please advise.

  • random_anonymousrandom_anonymous Posts: 7

    Is there a way to find the number of lines in a file? I am trying to write a python method for checking whether the last line in a file contains a certain a comment.

  • mstaronmstaron Posts: 7

    Maybe you should try to use the second rule (e.g. you have end line character after last comment). Without testing it is hard to advise something else.
    You can find the number of the last line in a file with the following rule:

    If you prefer python, then you can take the content of a file in a python method:

Sign In or Register to comment.