Skip to content

Commit

Permalink
Merge pull request #40204 from gartung/gartung-SA-const-cast-attr
Browse files Browse the repository at this point in the history
Utilities/StaticAnalyzers: Check for CMS_THREAD_SAFETY or CMS_SA_ALLOW attributes on statments using const_cast and skip warnings.
  • Loading branch information
cmsbuild committed Nov 30, 2022
2 parents 4c463fc + 5cfcf76 commit 0cc6517
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Utilities/StaticAnalyzers/src/ConstCastAwayChecker.cpp
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include <clang/AST/ExprCXX.h>
#include <clang/AST/Attr.h>
#include <clang/AST/ParentMap.h>

#include <memory>

Expand All @@ -22,6 +23,23 @@ namespace clangcms {
void ConstCastAwayChecker::checkPreStmt(const clang::ExplicitCastExpr *CE, clang::ento::CheckerContext &C) const {
if (!(clang::CStyleCastExpr::classof(CE) || clang::CXXConstCastExpr::classof(CE)))
return;
auto P = C.getCurrentAnalysisDeclContext()->getParentMap().getParent(CE);
while (!(isa<AttributedStmt>(P) || isa<DeclStmt>(P)) &&
C.getCurrentAnalysisDeclContext()->getParentMap().hasParent(P)) {
P = C.getCurrentAnalysisDeclContext()->getParentMap().getParent(P);
}
if (isa<AttributedStmt>(P)) {
const AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(P);
if (AS && (hasSpecificAttr<CMSSaAllowAttr>(AS->getAttrs()) || hasSpecificAttr<CMSThreadSafeAttr>(AS->getAttrs())))
return;
}
if (isa<DeclStmt>(P)) {
const DeclStmt *DS = dyn_cast_or_null<DeclStmt>(P);
if (DS && (hasSpecificAttr<CMSSaAllowAttr>(DS->getSingleDecl()->getAttrs()) ||
hasSpecificAttr<CMSThreadSafeAttr>(DS->getSingleDecl()->getAttrs())))
return;
}

const Expr *SE = CE->getSubExpr();
const CXXRecordDecl *CRD = nullptr;
std::string cname;
Expand Down
18 changes: 18 additions & 0 deletions Utilities/StaticAnalyzers/src/ConstCastChecker.cpp
Expand Up @@ -6,6 +6,7 @@

#include <clang/AST/Attr.h>
#include <clang/AST/ExprCXX.h>
#include <clang/AST/ParentMap.h>

#include <memory>

Expand All @@ -19,6 +20,23 @@ using namespace llvm;
namespace clangcms {

void ConstCastChecker::checkPreStmt(const clang::CXXConstCastExpr *CE, clang::ento::CheckerContext &C) const {
auto P = C.getCurrentAnalysisDeclContext()->getParentMap().getParent(CE);
while (!(isa<AttributedStmt>(P) || isa<DeclStmt>(P)) &&
C.getCurrentAnalysisDeclContext()->getParentMap().hasParent(P)) {
P = C.getCurrentAnalysisDeclContext()->getParentMap().getParent(P);
}
if (isa<AttributedStmt>(P)) {
const AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(P);
if (AS && (hasSpecificAttr<CMSSaAllowAttr>(AS->getAttrs()) || hasSpecificAttr<CMSThreadSafeAttr>(AS->getAttrs())))
return;
}
if (isa<DeclStmt>(P)) {
const DeclStmt *DS = dyn_cast_or_null<DeclStmt>(P);
if (DS && (hasSpecificAttr<CMSSaAllowAttr>(DS->getSingleDecl()->getAttrs()) ||
hasSpecificAttr<CMSThreadSafeAttr>(DS->getSingleDecl()->getAttrs())))
return;
}

const Expr *SE = CE->getSubExprAsWritten();
const CXXRecordDecl *CRD = nullptr;
std::string cname;
Expand Down

0 comments on commit 0cc6517

Please sign in to comment.