Using CAC++ as a developer
Introduction
To add a new verification, you have to follow all these steps, modifying each of the following files of the library:
Each of them is explained separately below.
caclibrary.h
Steps
- Add a method to the "checkCode" class to invoke the verification with suitable parameters to configure it.
- The last parameter should be the message to be printed when the verification fails. Provide a default message for it.
- Do not forget to document the verification, providing the signature, the utility of the verification, the goal of each parameter and examples of its usage.
Example:
/*
Method: void defaultConstructor(string className, bool exist, string message)
Utility: Method to verify that a class has a default constructor defined.
Parameters:
-string className (*): string with the name of the class to be searched.
- Example:
class A { A() { ... } };
class B { B() = default; };
class C { C(Type) { ... } };
defaultConstructor("A"); Pass
defaultConstructor("B"); Pass
defaultConstructor("C",false) Pass
defaultConstructor("C"); Fail
*/
void defaultConstructor(string className, bool exist = true, string message = "DEFAULT CONSTRUCTOR NOT FOUND.");
caclibrary.cpp
Steps
-
Define the "invocation" method to call the verification.
You should perform two main actions:
- Call a method that sets the information provided by the user in the member variable "e" (execution object). You will define this method, called the "set" method from now on, later in the class "execute" in "execute.cpp".
- Add a pattern (called matcher) to the member variable "Finder", passing the necessary arguments to parametrize it. You will define this pattern later in "matchers.cpp".
Example:
void checkCode::defaultConstructor(string className, bool exist, string notFoundMessage){
1) -> e.setDefaultConstructors(className, exist, notFoundMessage);
2) -> Finder.addMatcher(defaultConstructor_Matcher(className), &e);
}
matchers.cpp/.h
Steps
- Define the function associated with the pattern added to the member variable "Finder". Do not forget to add the declaration of this function to "matchers.h".
- Declare and return a variable of type "DeclarationMatcher".
- Use "bind" with a unique string identifier for those declarations that should be retrieved by the analyzer. You will see the use of these identifiers later in "execute.cpp" (point 2.1).
- You can find a complete list of the available matchers in the AST Matcher Reference of Clang.
Example:
DeclarationMatcher defaultConstructor_Matcher(string className){
DeclarationMatcher matcher =
cxxConstructorDecl(
isDefaultConstructor(),
unless(isImplicit()),
ofClass(
cxxRecordDecl(
matchesName(className),
unless(isTemplateInstantiation())
).bind("classWithDefaultConstructor")
)
).bind("defaultConstructor");
return matcher;
}
This matcher looks for declarations of constructors (cxxConstructorDecl), namely the default constructor (isDefaultConstructor), that belong to the class (ofClass - cxxRecordDecl) that matches the name provided by the user (matchesName(className)). The matcher "unless(isImplicit())" avoids taking into account implicit declarations added by the compiler. Similarly, the matcher "unless(isTemplateInstantiation())" avoids taking into account instantiations of a template expanded by the compiler.
This matcher binds both the constructor (with identifier "defaultConstructor") and the class (with identifier "classWithDefaultConstructor").
execute.h
Steps
You need to add four declarations to the class "execute":
-
A private "container" member variable of type "A", which will contain all the verifications of this kind added by the user.
Example:
A defaultConstructors; -
A public "set" method (see the steps in "caclibrary.cpp") to set the information provided by the user.
Example:
void setDefaultConstructors(string className, bool exist, string notFoundMessage); -
A private method to "process" each match retrieved by the analyzer.
Example:
void classWithDefaultConstructor(const MatchFinder::MatchResult &Result); -
A private method to "postprocess" the results of the search. This method should decide whether the message is displayed.
Example:
void hasDefaultConstructor();
execute.cpp
Steps
You should follow several steps in this file:
-
Define the "set" method to receive the information from "caclibrary.cpp". You should perform two main actions:
- Create a variable of type "Info" to save the information provided by the user. The class "Info" is explained later in info.h.
- Add the new verification to the "container". You should associate the object of class "Info" with a name that identifies the verification.
Example:
void execute::setDefaultConstructors(string className, bool exist, string notFoundMessage){ 1.1) -> Info info(className, notFoundMessage, false, exist); 1.2) -> defaultConstructors.insert(std::make_pair(className, info)); } -
Define the "process" method. This is the main method, and it will be automatically invoked each time the analyzer finds a location that meets the search pattern defined in "matchers.cpp". In general, you should perform the following four actions:
- Retrieve the declarations that were bound in the pattern with "bind".
- Check that those declarations are in valid locations (for instance, they are not in a system header).
- Search the "container" for the associated verification by means of the identifier used to identify the verifications in this container (see point 1.2). In the example below, the identifier is the name of the class.
- Finally, update the state of the "Info" object associated with this verification. In the example below, the member variable "found" in class "Info" is set to true to indicate that the default constructor was found.
Example:
void execute::classWithDefaultConstructor(const MatchFinder::MatchResult &Result){ 2.1) -> const CXXRecordDecl *foundClassWithDefaultConstructor = Result.Nodes.getNodeAs<clang::CXXRecordDecl>("classWithDefaultConstructor"); const CXXConstructorDecl *foundDefaultConstructor = Result.Nodes.getNodeAs<clang::CXXConstructorDecl>("defaultConstructor"); 2.2) -> if (existClass(foundClassWithDefaultConstructor) && existConstructor(foundDefaultConstructor)){ 2.3) -> for (AI it=defaultConstructors.begin(); it!=defaultConstructors.end(); ++it){ if(std::regex_match(foundClassWithDefaultConstructor->getNameAsString(), std::regex(it->first))){ 2.4) -> (it->second).setFound(true); } } } } -
Define the "postprocess" method. The functionality of this method may vary depending on the intention of the verification. Normally, you will simply display the message for those verifications that are not satisfied, as in the example below.
Example:
void execute::hasDefaultConstructor(){ if (!foundAll(defaultConstructors)){ getMessagesOfNotFound(defaultConstructors); correct = false; } } -
Include the invocation of the "process" method in the body of "run".
Example:
void execute::run(const MatchFinder::MatchResult &Result){ ... classWithDefaultConstructor(Result); ... } -
As a last step, include the invocation of the "postprocess" method in the body of "postRun":
Example:
void execute::postRun(){ ... hasDefaultConstructor(); ... }
info.h
This class already includes several member variables that you can reuse to save information for new verifications. However, if you need special variables not contemplated yet, you can add new ones.