CppDepend で Cert C++ コンプライアンスをシームレスに達成
CppDepend で Cert C++ コンプライアンスをシームレスに達成
今日の競争の激しいソフトウェア開発環境では、高品質で安全なコードの維持が不可欠です。Cert C++ コーディング標準は、信頼性が高く安全な C++ アプリケーションを作成するためのガイドラインとして機能します。しかし、開発者はプロジェクトで Cert C++ コンプライアンスをどのように効率的に達成できるでしょうか?
CppDepend の登場です。これは、Cert C++ 標準の複雑さをナビゲートするのを支援するために特別に設計された包括的なツールです。静的解析技術を活用することで、CppDepend はコンプライアンスへと導くだけでなく、コード全体の品質とセキュリティを向上させる力を与えてくれます。
CERT C++ 標準について学ぶには、これを印刷できます SEI CERT C++コーディング標準ドキュメント Aaron Ballmanによる。
CppDependでのCERT C++ルールの使用
新しい CppDepend プロジェクトを作成すると、分析に含めたいすべてのコーディング標準を表示するポップアップが表示されます。Cert C++ コーディング標準を含めるには、Cert オプションをチェックして Cert C++ を選択します。
プロジェクトを分析した後、Queries and Rules Explorer セクションに移動し、左側のメニューから Cert C++ Rules を選択します(下の画像で赤く強調表示)。これにより、関連するすべての Cert C++ ルールが右側に表示されます。ルールをクリックすると、対応する CQLinq クエリと関連するソースコードが自動的に生成されます。さらに、Metrics View セクションでは、問題の場所と重要性のビジュアライゼーションが提供されます。
CERT C++ ルールの HTML レポートへの組み込み
生成された HTML レポート(DevOps Edition で利用可能)に Cert C++ ルールを含めるには、Cert C++ グループを右クリックして「List Code Queries of this Group in a dedicated section in Report」を選択するだけです。
CERT C++ルール一覧
CppDepend は 38 の CERT C++ 標準クエリをサポートします。
各ルールとその機能をより深く理解するには、次を参照してください SEI CERT C++コーディング標準ドキュメント Aaron Ballmanによる。
宣言と初期化
DCL50-CPP: Do not define a C-style variadic function:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from m in **JustMyCode**.Methods where m.IsVariadic
select mDCL51-CPP: Do not declare or define a reserved identifier:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_pp_macro_is_reserved_id" select
new { issue,issue.FilePath,Line=issue.BeginLine}DCL52-CPP: Never qualify a reference type with const or volatile:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="err_invalid_reference_qualifier_application" select
new { issue,issue.FilePath,Line=issue.BeginLine}DCL53-CPP: Do not write syntactically ambiguous declarations:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_empty_parens_are_function_decl" select
new { issue,issue.FilePath,Line=issue.BeginLine}DCL54-CPP: Overload allocation and deallocation functions as a pair in the same scope:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from n in **JustMyCode**.Methods.Where(m=>m.SimpleName=="operatornew") where **JustMyCode**.Methods.Where(m=>m.SimpleName=="operatordelete").Count()==**0**
select nDCL57-CPP: Do not let exceptions escape from destructors or deallocation functions:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="exceptThrowInDestructor" select
new { issue,issue.FilePath,Line=issue.BeginLine}DCL58-CPP: Do not modify the standard namespaces:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from codeElement in **JustMyCode**.CodeElements where codeElement.Parents.Where(t=>t.IsNamespace && t.Name=="std").Count()>**0**
select codeElementDCL59-CPP: Do not define an unnamed namespace in a header file:
このコーディング標準は次のクエリで計算されます:
warnif **count**>**0** from n in **Namespaces**
where n.Name.StartsWith("anonymous_namespace") && n.Name.Contains(".h")
select n式 (Exp)
EXP50-CPP: Do not depend on the order of evaluation for side effects:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_unsequenced_mod_use" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP52-CPP: Do not rely on side effects in unevaluated operands:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_side_effects_unevaluated_context" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP53-CPP: 未初期化メモリを読まないこと:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && (issue.Type=="uninitvar" || issue.Type=="uninitdata") select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP54-CPP: Do not access an object outside of its lifetime:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_ret_stack_addr_ref" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP55-CPP: Do not access a cv-qualified object through a cvunqualified type:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from m in **JustMyCode**.Methods where
m.IsUsing("Keywords.const_cast".AllowNoMatch()) select mEXP57-CPP: Do not cast or delete pointers to incomplete classes:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_delete_incomplete" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP58-CPP: Pass an object of the correct type to va\_start:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_va_start_type_is_undefined" select
new { issue,issue.FilePath,Line=issue.BeginLine}EXP61-CPP: A lambda object must not outlive any of its reference captured objects:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="returnDanglingLifetime"
&& issue.Description.Contains(("lambda")) select new { issue,issue.FilePath,Line=issue.BeginLine}EXP62-CPP: Do not access the bits of an object representation that are not part of the object’s value representation:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_dyn_class_memaccess" select
new { issue,issue.FilePath,Line=issue.BeginLine}メモリ管理 (MEM)
MEM50-CPP: 解放されたメモリにアクセスしないこと:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="nullPointer"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}MEM51-CPP:Properly deallocate dynamically allocated resources:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_mismatched_delete_new"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}MEM53-CPP. Explicitly construct and destruct objects when manually managing object lifetime:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="uninitdata"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}MEM56-CPP:Do not store an already-owned pointer value in an unrelated smart pointer:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="doubleFree"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}MEM57-CPP: Avoid using default operator new for over-aligned types:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-mem57-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}例外とエラー処理 (ERR)
ERR51-CPP: すべての例外を処理:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0**
from m in **JustMyCode**.Methods where m.IsEntryPoint
&& m.IsUsing("Keywords.generic_catch".AllowNoMatch()) select mERR52-CPP: setjmp()やlongjmp()を使用しないこと:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="cert-err52-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}ERR53-CPP: Do not reference base classes or class data members in a constructor or destructor function-try-block handler:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang"
&& issue.Type=="warn_cdtor_function_try_handler_mem_expr"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}ERR54-CPP: Catch handlers should order their parameter types from most derived to least derived:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang"
&& issue.Type=="warn_exception_caught_by_earlier_handler"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}ERR58-CPP. Handle all exceptions thrown before main() begins executing:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-err58-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}ERR60-CPP: Exception objects must be nothrow copynconstructible:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-err60-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}ERR61-CPP: Catch exceptions by lvalue reference:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="catchExceptionByValue"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}オブジェクト指向プログラミング (OOP)
OOP50-CPP: Do not invoke virtual functions from constructors or destructors:
このコーディング標準は次のクエリで計算されます:
from m in **JustMyCode**.Methods.Where(m=>(m.IsConstructor || m.IsDestructor)
&& m.MethodsCalled.Where(s=>s.IsVirtual).Count()>**0**)
select mOOP52-CPP: Do not delete a polymorphic object without a virtual destructor:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_delete_non_virtual_dtor"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}OOP53-CPP: Write constructor member initializers in the canonical order:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_initializer_out_of_order"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}OOP54-CPP: Gracefully handle self-copy assignment:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="CppCheck" && issue.Type=="operatorEqToSelf"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}OOP57-CPP. Prefer special member functions and overloaded operators to C Standard Library functions:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-oop57-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}OOP58-CPP: Copy operations must not mutate the source object:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-oop58-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}その他 (MSC)
MSC50-CPP: Do not use std::rand() for generating pseudorandom numbers:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-msc50-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}MSC51-CPP. Ensure your random number generator is properlyseeded:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang-Tidy" && issue.Type=="cert-msc51-cpp"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}コンテナ
CTR50-CPP: Guarantee that container indices and iterators are within the valid range:
このコーディング標準は次のクエリで計算されます:
warnif **count** > **0** from issue in **ImportedIssues** where issue.ToolName=="Clang" && issue.Type=="warn_array_index_exceeds_bounds"
select new { issue, issue.FilePath, Line = issue.BeginLine ,
Debt = **10**.ToMinutes().ToDebt(),
Severity =Severity.High
}