演示示例
以下过程演示如何创建演练:对 C/C++ 代码进行缺陷分析所对应的示例。 这些过程将创建:
一个名为 CppDemo 的 Visual Studio 解决方案。
一个名为 CodeDefects 的静态库项目。
一个名为 Annotations 的静态库项目。
这些过程还提供静态库的头文件和 .cpp 文件的代码。
创建 CppDemo 解决方案和 CodeDefects 项目
单击**“文件”菜单,指向“新建”,然后单击“新建项目”**。
在**“项目类型”树列表中,如果 VS 中的默认语言不是 Visual C++,请展开“其他语言”**。
展开**“Visual C++”,然后单击“常规”**。
在**“模板”中,单击“空项目”**。
在**“名称”**文本框中,键入 CodeDefects。
选中**“创建解决方案的目录”**复选框。
在**“解决方案名称”**文本框中,键入 CppDemo。
将 CodeDefects 项目配置为静态库
在解决方案资源管理器中,右击**“CodeDefects”,然后单击“属性”**。
展开**“配置属性”,然后单击“常规”**。
在**“常规”列表中,选择“目标文件扩展名”**旁边列中的文本,然后键入 .lib。
在**“项目默认值”中,单击“配置类型”旁边的列,然后单击“静态库(.lib)”**。
将头文件和源文件添加到 CodeDefects 项目中
在解决方案资源管理器中,展开**“CodeDefects”,右击“头文件”,单击“添加”,再单击“新建项”**。
在**“添加新项”对话框中,单击“代码”,再单击“头文件(.h)”**。
在**“名称”框中,键入 Bug.cpp,然后单击“添加”**。
复制下面的代码并将其粘贴到 Visual Studio 编辑器中的**“Bug.cpp”**文件。
#include <windows.h> // //These 3 functions are consumed by the sample // but are not defined. This project cannot be linked! // bool CheckDomain( LPCSTR ); HRESULT ReadUserAccount(); // //These constants define the common sizes of the // user account information throughout the program // const int USER_ACCOUNT_LEN = 256; const int ACCOUNT_DOMAIN_LEN = 128;
在解决方案资源管理器中,右击**“源文件”,指向“新建”,然后单击“新建项”**。
在**“添加新项”对话框中,单击“C++ 文件(.cpp)”**。
在**“名称”框中,键入 Bug.cpp,然后单击“添加”**。
复制下面的代码并将其粘贴到 Visual Studio 编辑器中的 Bug.h 文件。
#include <stdlib.h> #include "Bug.h" // the user account TCHAR g_userAccount[USER_ACCOUNT_LEN] = ""; int len = 0; bool ProcessDomain() { TCHAR* domain = new TCHAR[ACCOUNT_DOMAIN_LEN]; // ReadUserAccount gets a 'domain\user' input from //the user into the global 'g_userAccount' if (ReadUserAccount() ) { // Copies part of the string prior to the '\' // character onto the 'domain' buffer for( len = 0 ; (len < ACCOUNT_DOMAIN_LEN) && (g_userAccount[len] != '\0') ; len++ ) { if ( g_userAccount[len] == '\\' ) { // Stops copying on the domain and user separator ('\') break; } domain[len] = g_userAccount[len]; } if((len= ACCOUNT_DOMAIN_LEN) || (g_userAccount[len] != '\\')) { // '\' was not found. Invalid domain\user string. delete [] domain; return false; } else { domain[len]='\0'; } // Process domain string bool result = CheckDomain( domain ); delete[] domain; return result; } return false; } int path_dependent(int n) { int i; int j; if (n == 0) i = 1; else j = 1; return i+j; }
单击**“文件”菜单,然后单击“全部保存”**。
添加 Annotations 项目并将其配置为静态库
在解决方案资源管理器中,单击**“CppDemo”,指向“添加”,然后单击“新建项目”**。
在**“添加新项目”对话框中,展开“Visual C++”,单击“常规”,再单击“空项目”**。
在**“名称”文本框中,键入 Annotations,然后单击“添加”**。
在解决方案资源管理器中,右击**“Annotations”,然后单击“属性”**。
展开**“配置属性”,然后单击“常规”**。
在**“常规”列表中,选择“目标文件扩展名”**旁边列中的文本,然后键入 .lib。
在**“项目默认值”中,单击“配置类型”旁边的列,然后单击“静态库(.lib)”**。
将头文件和源文件添加到 Annotations 项目中
在解决方案资源管理器中,展开**“Annotations”,右击“头文件”,再单击“添加”,然后单击“新建项”**。
在**“添加新项”对话框中,单击“头文件(.h)”**。
在**“名称”框中,键入 annotations.h,然后单击“添加”**。
复制下面的代码并将其粘贴到 Visual Studio 编辑器中的 annotations.h 文件。
#include <CodeAnalysis/SourceAnnotations.h> struct LinkedList { struct LinkedList* next; int data; }; typedef struct LinkedList LinkedList; [returnvalue:SA_Post( Null=SA_Maybe )] LinkedList* AllocateNode();
在解决方案资源管理器中,右击**“源文件”,指向“新建”,然后单击“新建项”**。
在**“添加新项”对话框中,单击“代码”,然后单击“C++ 文件(.cpp)”**。
在**“名称”框中键入 annotations.cpp,再单击“添加”**。
复制下面的代码并将其粘贴到 Visual Studio 编辑器中的 annotations.cpp 文件。
#include <CodeAnalysis/SourceAnnotations.h> #include <windows.h> #include <stdlib.h> #include "annotations.h" LinkedList* AddTail( LinkedList *node, int value ) { LinkedList *newNode = NULL; // finds the last node while ( node->next != NULL ) { node = node->next; } // appends the new node newNode = AllocateNode(); newNode->data = value; newNode->next = 0; node->next = newNode; return newNode; }
单击**“文件”菜单,然后单击“全部保存”**。