Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:32:05

0001 //== StaticLocalChecker.cpp - Checks for non-const static locals --------------*- C++ -*--==//
0002 //
0003 // by Thomas Hauth [ Thomas.Hauth@cern.ch ]
0004 //
0005 //===----------------------------------------------------------------------===//
0006 
0007 #include "StaticLocalChecker.h"
0008 
0009 #include "CmsSupport.h"
0010 #include <iostream>
0011 #include <clang/AST/Attr.h>
0012 using namespace clang;
0013 using namespace ento;
0014 using namespace llvm;
0015 
0016 namespace clangcms {
0017 
0018   void StaticLocalChecker::checkASTDecl(const clang::VarDecl *D,
0019                                         clang::ento::AnalysisManager &Mgr,
0020                                         clang::ento::BugReporter &BR) const {
0021     clang::QualType t = D->getType();
0022     if (D->hasAttr<CMSThreadGuardAttr>() || D->hasAttr<CMSThreadSafeAttr>() || D->hasAttr<CMSSaAllowAttr>())
0023       return;
0024     if (((D->isStaticLocal() || D->isStaticDataMember()) &&
0025          D->getTSCSpec() != clang::ThreadStorageClassSpecifier::TSCS_thread_local) &&
0026         !support::isConst(t)) {
0027       clang::ento::PathDiagnosticLocation DLoc =
0028           clang::ento::PathDiagnosticLocation::createBegin(D, BR.getSourceManager());
0029 
0030       if (!m_exception.reportGlobalStaticForType(t, DLoc, BR))
0031         return;
0032       if (support::isSafeClassName(t.getCanonicalType().getAsString()))
0033         return;
0034 
0035       std::string buf;
0036       llvm::raw_string_ostream os(buf);
0037       os << "Non-const variable '" << t.getAsString() << " " << *D
0038          << "' is static local or static member data and might be thread-unsafe";
0039 
0040       BR.EmitBasicReport(D, this, "non-const static variable", "ThreadSafety", os.str(), DLoc);
0041       return;
0042     }
0043   }
0044 
0045 }  // namespace clangcms