/// Takes a list of diagnostics that were expected to have been generated
/// but were not and produces a diagnostic to the user from this.
static unsigned PrintExpected(DiagnosticsEngine &Diags,
                              SourceManager &SourceMgr,
                              std::vector<Directive *> &DL, const char *Kind) {
  if (DL.empty())
    return 0;

  SmallString<256> Fmt;
  llvm::raw_svector_ostream OS(Fmt);
  for (const auto *D : DL) {
    if (D->DiagnosticLoc.isInvalid())
      OS << "\n  File *";
    else
      OS << "\n  File " << SourceMgr.getFilename(D->DiagnosticLoc);
    if (D->MatchAnyLine)
      OS << " Line *";
    else
      OS << " Line " << SourceMgr.getPresumedLineNumber(D->DiagnosticLoc);
    if (D->DirectiveLoc != D->DiagnosticLoc)
      OS << " (directive at "
         << SourceMgr.getFilename(D->DirectiveLoc) << ':'
         << SourceMgr.getPresumedLineNumber(D->DirectiveLoc) << ')';
    OS << ": " << D->Text;
  }

  Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
    << Kind << /*Unexpected=*/false << OS.str();
  return DL.size();
}
ClangTidyMessage::ClangTidyMessage(StringRef Message,
                                   const SourceManager &Sources,
                                   SourceLocation Loc)
    : Message(Message) {
  FilePath = Sources.getFilename(Loc);
  FileOffset = Sources.getFileOffset(Loc);
}
ClangTidyMessage::ClangTidyMessage(StringRef Message,
                                   const SourceManager &Sources,
                                   SourceLocation Loc)
    : Message(Message) {
  assert(Loc.isValid() && Loc.isFileID());
  FilePath = Sources.getFilename(Loc);
  FileOffset = Sources.getFileOffset(Loc);
}
示例#4
0
std::string toString(SourceManager& sourceManager, SourceLocation loc) {
    //return loc.printToString(sourceManager);
    std::string fileName = sourceManager.getFilename(loc).str();
    if (fileName.length() > 30)
        fileName = fileName.substr(fileName.length() - 30);
    std::ostringstream os;
    os << fileName << ":" <<
        sourceManager.getExpansionLineNumber(loc) << ":" <<
        sourceManager.getExpansionColumnNumber(loc);
    return os.str();
}
示例#5
0
template <typename T> FullSourceRange createSourceRangeForStmt(const T *S, SourceManager &sourceManager) {
  FullSourceRange sourceRange;

  sourceRange.startingLineNumber = sourceManager.getSpellingLineNumber(S->getLocStart());
  sourceRange.endingLineNumber = sourceManager.getSpellingLineNumber(S->getLocEnd());
  sourceRange.startingColumnNumber = sourceManager.getSpellingColumnNumber(S->getLocStart());
  sourceRange.endingColumnNumber = sourceManager.getSpellingColumnNumber(S->getLocEnd());
  sourceRange.filePath = sourceManager.getFilename(sourceManager.getSpellingLoc(S->getLocStart()));

  return sourceRange;
}
/// \brief Takes a list of diagnostics that were expected to have been generated
/// but were not and produces a diagnostic to the user from this.
static unsigned PrintExpected(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
                              DirectiveList &DL, const char *Kind) {
  if (DL.empty())
    return 0;

  SmallString<256> Fmt;
  llvm::raw_svector_ostream OS(Fmt);
  for (DirectiveList::iterator I = DL.begin(), E = DL.end(); I != E; ++I) {
    Directive &D = **I;
    OS << "\n  File " << SourceMgr.getFilename(D.DiagnosticLoc)
          << " Line " << SourceMgr.getPresumedLineNumber(D.DiagnosticLoc);
    if (D.DirectiveLoc != D.DiagnosticLoc)
      OS << " (directive at "
         << SourceMgr.getFilename(D.DirectiveLoc) << ':'
         << SourceMgr.getPresumedLineNumber(D.DirectiveLoc) << ')';
    OS << ": " << D.Text;
  }

  Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit()
    << Kind << /*Unexpected=*/false << OS.str();
  return DL.size();
}
  bool FindSymbol(ASTContext &Context, const SourceManager &SourceMgr,
                  unsigned SymbolOffset, const std::string &QualifiedName) {
    DiagnosticsEngine &Engine = Context.getDiagnostics();

    const SourceLocation Point =
        SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID())
            .getLocWithOffset(SymbolOffset);

    if (!Point.isValid()) {
      ErrorOccurred = true;
      unsigned InvalidOffset = Engine.getCustomDiagID(
          DiagnosticsEngine::Error,
          "SourceLocation in file %0 at offset %1 is invalid");
      Engine.Report(Point, InvalidOffset) << SourceMgr.getFilename(Point)
                                          << SymbolOffset;
      return false;
    }

    const NamedDecl *FoundDecl = QualifiedName.empty()
                                     ? getNamedDeclAt(Context, Point)
                                     : getNamedDeclFor(Context, QualifiedName);

    if (FoundDecl == nullptr) {
      if (QualifiedName.empty()) {
        FullSourceLoc FullLoc(Point, SourceMgr);
        unsigned CouldNotFindSymbolAt = Engine.getCustomDiagID(
            DiagnosticsEngine::Error,
            "clang-rename could not find symbol (offset %0)");
        Engine.Report(Point, CouldNotFindSymbolAt) << SymbolOffset;
        ErrorOccurred = true;
        return false;
      }
      unsigned CouldNotFindSymbolNamed = Engine.getCustomDiagID(
          DiagnosticsEngine::Error, "clang-rename could not find symbol %0");
      Engine.Report(CouldNotFindSymbolNamed) << QualifiedName;
      ErrorOccurred = true;
      return false;
    }

    // If FoundDecl is a constructor or destructor, we want to instead take
    // the Decl of the corresponding class.
    if (const auto *CtorDecl = dyn_cast<CXXConstructorDecl>(FoundDecl))
      FoundDecl = CtorDecl->getParent();
    else if (const auto *DtorDecl = dyn_cast<CXXDestructorDecl>(FoundDecl))
      FoundDecl = DtorDecl->getParent();

    SpellingNames.push_back(FoundDecl->getNameAsString());
    AdditionalUSRFinder Finder(FoundDecl, Context);
    USRList.push_back(Finder.Find());
    return true;
  }
示例#8
0
    bool VisitCallExpr(CallExpr* call) {
        //outs() << "do found" << "\n";
        NamedDecl* calleedecl = (NamedDecl*)call->getCalleeDecl();
        if (calleedecl != NULL) {
            //ignore macro expansion
            SourceLocation loc = call->getLocStart();
            if (!sm->isInMainFile(loc))
                return true;

            if (call->getCalleeDecl()->isFunctionOrFunctionTemplate()) {
                SourceRange sr = call->getCalleeDecl()->getAsFunction()->getSourceRange();
                SourceLocation start_pos = sr.getBegin();
                if (!start_pos.isValid()) {
                    //outs() << "error: invalid location." << "\n";
                    //outfile << "error: invalid location." << "\n";
                    return true;
                }
                else {
                    StringRef headerFilename = sm->getFilename(start_pos);
                    string fullheaderfile = headerFilename.str();
                    if (fullheaderfile == "")
                        return true;
                    if (fullheaderfile.find(project_name) < fullheaderfile.length())
                        return true;
                    if (fullheaderfile[0] != '/')
                        return true;
                    /*
                    int i;
                    for (i = fullheaderfile.length() - 1; i >=0; i--)
                      if (fullheaderfile[i] == '/')
                        break;
                    string headerfile;
                    if (fullheaderfile.substr(fullheaderfile.length()-2, 2) == ".h")
                      headerfile = fullheaderfile.substr(i+1, fullheaderfile.length() - i - 3);
                    else
                      headerfile = fullheaderfile.substr(i+1, fullheaderfile.length() - i - 1);
                    bool flag = false;
                    for (int j = 0; j < API_NUM; j++)
                    {
                      bool flag2 = true;
                      for (int k = 0; k < headerfile.length(); k++){
                        if (headerfile[k] != APIList[j][k])
                    flag2 = false;
                      }
                      if (flag2 == true){
                        flag = true;
                      }
                    }
                    if (!flag)
                      return true;
                    */
                }
            }
            else {
                //return true;
            }

            outs() << "Found " << calleedecl->getQualifiedNameAsString() <<" at: ";
            //outfile << "Found " << calleedecl->getQualifiedNameAsString() <<" at: ";
            string s = calleedecl->getQualifiedNameAsString();
            qualified_name = "";
            short_name = "";
            unsigned int pos = s.length()-1;
            while (pos > 0) {
                if (s[pos] == ':' && s[pos-1] == ':')
                    break;
                pos--;
            }
            if (pos == 0)
                pos = -1;
            short_name = s.substr(pos+1, s.length()-pos-1);
            int mark = 0;
            for (unsigned int i = 0; i < s.length(); i++) {
                if (s[i] == ',')
                    s[i] = ';';
                if (s[i] == '<') {
                    mark ++;
                }
                if (i > 0) {
                    if (s[i-1] == '>') {
                        mark --;
                    }
                }
                if (mark == 0 || i >= pos+1)
                    qualified_name = qualified_name + s[i];
            }

            StringRef filename = sm->getFilename(loc);
            //unsigned int col = sm->getSpellingColumnNumber(loc);
            //unsigned int line = sm->getSpellingLineNumber(loc);
            outs() << filename << "\n";
            //file_name = filename.str();
            //outfile << filename.str() << "\n";
            //outfile <<filename.str();
            //outs() << ", line: " <<line<< " column: " << col << "\n";
            //outfile << ", line: " <<line<< " column: " << col << "\n";

            //get parent function
            if (CurrentFunc != NULL) {
                SourceRange sr = CurrentFunc->getSourceRange();
                SourceLocation start_pos = sr.getBegin();
                SourceLocation end_pos = sr.getEnd();
                start_line = sm->getSpellingLineNumber(start_pos);
                end_line = sm->getSpellingLineNumber(end_pos);
                outs() << "Function:" << CurrentFunc->getNameInfo().getAsString() <<", StartLine: " <<start_line<< " EndLine: " << end_line << "\n";
                //outfile << "Function:" << CurrentFunc->getNameInfo().getAsString() <<", StartLine: " <<start_line<< " EndLine: " << end_line << "\n";
            }
            else {
                outs() << "error: no parent func" << "\n";
                //outfile << "error: no parent func" << "\n";
            }

            //get header file
            if (call->getCalleeDecl()->isFunctionOrFunctionTemplate()) {
                SourceRange sr = call->getCalleeDecl()->getAsFunction()->getSourceRange();
                SourceLocation start_pos = sr.getBegin();
                if (!start_pos.isValid()) {
                    outs() << "error: invalid location." << "\n";
                    return true;
                    //outfile << "error: invalid location." << "\n";
                }
                else {
                    StringRef headerFilename = sm->getFilename(start_pos);
                    outs() << "From declaration file:" << headerFilename<< ", line " << sm->getSpellingLineNumber(start_pos) <<"\n";
                    //outfile << "From declaration file:" << headerFilename.str()<< ", line " << sm->getSpellingLineNumber(start_pos) <<"\n";
                    header_file = headerFilename.str();
                }
            }
            else {
                outs() << "error: header file not found(callee is not a function)" << "\n";
                return true;
                //outfile << "error: header file not found(callee is not a function)" << "\n";
            }
            outs() << "\n";
            //outfile << "\n";
            full_file_name = "D:/Code/Repos/r10k_cpp"+file_name;
            if (end_line < start_line)
                return true;
            if (end_line-start_line< 8)
                ranks = -(end_line-start_line-8);
            else
                ranks = (end_line-start_line-8);
            ranks = ranks / 3;
            //if (header_file != ""){
            outfile<<short_name<<","<<qualified_name<<","<<full_file_name<<","<<start_line<<","<<end_line<<","<<ranks<<","<<project_name<<endl;

            //outfile<<project_name<<","<<file_name<<","<<qualified_name<<","<<short_name<<","<<header_file<<","<<start_line<<","<<end_line<<endl;
            //}
            return true;
        } else {
            //outs() << "null" << "\n";
            return true;
        }
    }