Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

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_anonymous
    random_anonymous Posts: 7
    edited March 2018

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

  • mstaron
    mstaron Posts: 35

    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.

  • mstaron
    mstaron Posts: 35

    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

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

  • mstaron
    mstaron Posts: 35

    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:

  • [Deleted User]
    [Deleted User] Posts: 0 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"

  • @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.

  • 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.

  • mstaron
    mstaron Posts: 35

    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: