コード例 #1
0
ファイル: cibidmgr.cpp プロジェクト: satya-das/cib
bool CibIdMgr::loadIds(const std::string& idsFilePath)
{
  CppParser cppparser;
  auto      idCmp = cppparser.parseFile(idsFilePath.c_str());
  if (idCmp == nullptr)
    return false;
  std::unordered_map<CibFullClassNsName, CibFullClassName> nsNameToNameMap;
  traverse(idCmp, [&](const CppObj* cppObj) -> bool {
    if (cppObj->objType_ == CppObjType::kEnum)
    {
      auto* enumObj = static_cast<const CppEnum*>(cppObj);
      if (!enumObj->itemList_)
        return false;
      static const std::string kClassIdName     = "__zz_cib_classid";
      static const std::string kMethodIdName    = "__zz_cib_methodid";
      static const std::string kNextClsIdName   = "__zz_cib_next_class_id";
      auto                     extractClassName = [](const CppCompound* classObj) -> std::string {
        if (!classObj->members().empty() && (classObj->members().front()->objType_ == CppObjType::kDocComment))
        {
          static const std::string kClsNameCommentStart = "//#= FullClassName: ";
          const CppDocCommentEPtr  cmnt                 = classObj->members().front();
          auto                     startPos             = cmnt->doc_.find(kClsNameCommentStart);
          if (startPos != std::string::npos)
            return cmnt->doc_.substr(startPos + kClsNameCommentStart.size());
        }
        return "";
      };
      auto extractClassNsName = [](const CppCompound* classObj) -> std::string {
        return fullName(classObj).substr(11); // skip "__zz_cib_::"
      };
      if (cppObj->owner()->name() == kMethodIdName)
      {
        auto classNsName = extractClassNsName(cppObj->owner()->owner());
        auto itr         = nsNameToNameMap.find(classNsName);
        if (itr != nsNameToNameMap.end())
          loadMethodIds(itr->second, static_cast<const CppEnum*>(cppObj));
      }
      else if (enumObj->itemList_->front()->name_ == kClassIdName)
      {
        auto clsName   = extractClassName(enumObj->owner());
        auto clsNsName = extractClassNsName(enumObj->owner());
        auto clsId     = parseIdExpression(enumObj->itemList_->front()->val_);
        if (clsId != 0)
          addClass(clsName, clsNsName, clsId);
        if (!clsName.empty() && !clsNsName.empty())
          nsNameToNameMap[clsNsName] = clsName;
      }
      else if (enumObj->itemList_->front()->name_ == kNextClsIdName)
      {
        auto nextClsId = parseIdExpression(enumObj->itemList_->front()->val_);
        if (nextClassId_ < nextClsId)
          nextClassId_ = nextClsId;
      }
    }
    return false;
  });
  return true;
}
コード例 #2
0
int main(int argc, char *argv[])
{
	string inPath, outPath;	// in - source code, out - xml report
	if(!processCmdLineArgs(argc, argv, inPath, outPath))
		return 0;
	//
	cout << "Parsing...";
	CppParser cpp;
	Project *project = cpp.parseFile(inPath);
	if(cpp.errorOccured())
	{
		cout << "Error!" << endl;
		return 1;
	}
	cout << "Ok." << endl;
	//
#if YYDEBUG == 1
	//
	// TinyXML test
	TiXmlDocument doc;
	TiXmlDeclaration *decl = new TiXmlDeclaration("1.0", "utf-8", "yes");
	doc.LinkEndChild(decl);
	//
	//TiXmlStylesheetReference *style = new TiXmlStylesheetReference("text/xsl", "test.xsl");
	//doc.LinkEndChild(style);
	//
	TiXmlElement *root = new TiXmlElement("ASTM");
	project->dumpXml(root);
	doc.LinkEndChild(root);
	//
	doc.SaveFile("astm_dump.xml");
	//
#endif	// YYDEBUG == 1
	//
	cout << "Analysing...";
	//
	LinesOfCode loc(project->files[0]);
	CommentsDensity commdens(loc);
	CyclomaticComplexityNumber ccn(project->files[0]);
	DecisionDensity decdens(loc, ccn);
	NPathComplexity npath(project->files[0]);
	TotalFunctionCalls tfc(project->files[0]);
	NumberOfClasses noc(project->files[0]);
	LorenzKidd lk(project->files[0]);
	//
	string resultsFile = (outPath.size() > 0) ? outPath : "report.xml";
	ofstream fout;
	fout.open(resultsFile.c_str());
	if(!fout.is_open())
	{
		cout << "Error writing the results!" << endl;
		return 2;
	}
	fout << "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
		 << "<?xml-stylesheet href=\"sw_metrics.xsl\" type=\"text/xml\"?>\n"
		 << "<sw_stats>\n"
		 << "\t<file>" << inPath << "</file>\n"
		 << loc.dumpResultXml()
		 << commdens.dumpResultXml()
		 << ccn.dumpResultXml()
		 << decdens.dumpResultXml()
		 << npath.dumpResultXml()
		 << tfc.dumpResultXml()
		 << noc.dumpResultXml()
		 << lk.dumpResultXml()
		 << "</sw_stats>\n";
	//
	fout.close();
	cout << "Ok." << endl;
	cout << "Results written into '" << resultsFile << "'." << endl;
	//
	//delete project;
	return 0;
}