コード例 #1
0
bool FindPatternVisitor::VisitFunctionDecl (FunctionDecl* Declaration){
    
    if(!Declaration)
        return true;
    
    if(!(Declaration->isThisDeclarationADefinition() && Declaration->hasBody()))
        return true;
    
    FullSourceLoc FullLocation = CI->getASTContext().getFullLoc(Declaration->getLocStart());
    
    if(!FullLocation.isValid())
        return true;
    
    ///the function is located in head file
    if(FullLocation.getExpansionLoc().getFileID() != CI->getSourceManager().getMainFileID())
        return true;
    
    //llvm::errs()<<"Found function "<<Declaration->getQualifiedNameAsString() ;
    //llvm::errs()<<" @ " << FullLocation.printToString(FullLocation.getManager()) <<"\n";
    
    hasRecorded.clear();
    
    if(Stmt* function = Declaration->getBody()){
        travelStmt(function, function);
    }
    
    return true;
}
コード例 #2
0
 bool VisitCXXRecordDecl(CXXRecordDecl *Declaration) {
   if (Declaration->getQualifiedNameAsString() == "n::m::C") {
     FullSourceLoc FullLocation = Context->getFullLoc(Declaration->getLocStart());
     if (FullLocation.isValid())
       llvm::outs() << "Found declaration at "
                    << FullLocation.getSpellingLineNumber() << ":"
                    << FullLocation.getSpellingColumnNumber() << "\n";
   }
   return true;
 }
コード例 #3
0
ファイル: fecha_check.cpp プロジェクト: Jeffresh/POO
  void apply_overloaded_compare(const MatchFinder::MatchResult &Result){
    //Solo entrara para la definición, ya que en el matcher exigimos que use en el cuerpo "!=" o "<"
    if (const FunctionDecl *FS = Result.Nodes.getNodeAs<clang::FunctionDecl>("overloaded_compare")){
		
      FullSourceLoc FullLocation = Context->getFullLoc(FS->getLocStart());
      		
      if (FullLocation.isValid() 
	  && !Context->getSourceManager().isInSystemHeader(FullLocation) 
	){

	//llvm::outs() << "Nombre del método compare: " << FS->getNameAsString() << "\n";
	map<string, bool>::iterator it;
	if( (it = operators_compare.find(FS->getNameAsString())) != operators_compare.end() ){
	  it->second = true;
	}
      }
    }	
  }
コード例 #4
0
ファイル: Diagnostic.cpp プロジェクト: Celtoys/clReflect
DiagnosticsEngine::DiagStatePointsTy::iterator
DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
  assert(!DiagStatePoints.empty());
  assert(DiagStatePoints.front().Loc.isInvalid() &&
         "Should have created a DiagStatePoint for command-line");

  FullSourceLoc Loc(L, *SourceMgr);
  if (Loc.isInvalid())
    return DiagStatePoints.end() - 1;

  DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
  FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
  if (LastStateChangePos.isValid() &&
      Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
    Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
                           DiagStatePoint(0, Loc));
  --Pos;
  return Pos;
}
コード例 #5
0
ファイル: TextDiagnostic.cpp プロジェクト: Teemperor/clang
void TextDiagnostic::emitDiagnosticMessage(
    FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level,
    StringRef Message, ArrayRef<clang::CharSourceRange> Ranges,
    DiagOrStoredDiag D) {
  uint64_t StartOfLocationInfo = OS.tell();

  // Emit the location of this particular diagnostic.
  if (Loc.isValid())
    emitDiagnosticLoc(Loc, PLoc, Level, Ranges);

  if (DiagOpts->ShowColors)
    OS.resetColor();

  printDiagnosticLevel(OS, Level, DiagOpts->ShowColors,
                       DiagOpts->CLFallbackMode);
  printDiagnosticMessage(OS,
                         /*IsSupplemental*/ Level == DiagnosticsEngine::Note,
                         Message, OS.tell() - StartOfLocationInfo,
                         DiagOpts->MessageLength, DiagOpts->ShowColors);
}
コード例 #6
0
void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
                                  const PathDiagnosticPiece& P,
                                  unsigned num, unsigned max) {

  // For now, just draw a box above the line in question, and emit the
  // warning.
  FullSourceLoc Pos = P.getLocation().asLocation();

  if (!Pos.isValid())
    return;

  SourceManager &SM = R.getSourceMgr();
  assert(&Pos.getManager() == &SM && "SourceManagers are different!");
  std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedExpansionLoc(Pos);

  if (LPosInfo.first != BugFileID)
    return;

  const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
  const char* FileStart = Buf->getBufferStart();

  // Compute the column number.  Rewind from the current position to the start
  // of the line.
  unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
  const char *TokInstantiationPtr =Pos.getExpansionLoc().getCharacterData();
  const char *LineStart = TokInstantiationPtr-ColNo;

  // Compute LineEnd.
  const char *LineEnd = TokInstantiationPtr;
  const char* FileEnd = Buf->getBufferEnd();
  while (*LineEnd != '\n' && LineEnd != FileEnd)
    ++LineEnd;

  // Compute the margin offset by counting tabs and non-tabs.
  unsigned PosNo = 0;
  for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
    PosNo += *c == '\t' ? 8 : 1;

  // Create the html for the message.

  const char *Kind = 0;
  switch (P.getKind()) {
  case PathDiagnosticPiece::Call:
      llvm_unreachable("Calls should already be handled");
  case PathDiagnosticPiece::Event:  Kind = "Event"; break;
  case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
    // Setting Kind to "Control" is intentional.
  case PathDiagnosticPiece::Macro: Kind = "Control"; break;
  }

  std::string sbuf;
  llvm::raw_string_ostream os(sbuf);

  os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";

  if (num == max)
    os << "EndPath";
  else
    os << "Path" << num;

  os << "\" class=\"msg";
  if (Kind)
    os << " msg" << Kind;
  os << "\" style=\"margin-left:" << PosNo << "ex";

  // Output a maximum size.
  if (!isa<PathDiagnosticMacroPiece>(P)) {
    // Get the string and determining its maximum substring.
    const std::string& Msg = P.getString();
    unsigned max_token = 0;
    unsigned cnt = 0;
    unsigned len = Msg.size();

    for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
      switch (*I) {
      default:
        ++cnt;
        continue;
      case ' ':
      case '\t':
      case '\n':
        if (cnt > max_token) max_token = cnt;
        cnt = 0;
      }

    if (cnt > max_token)
      max_token = cnt;

    // Determine the approximate size of the message bubble in em.
    unsigned em;
    const unsigned max_line = 120;

    if (max_token >= max_line)
      em = max_token / 2;
    else {
      unsigned characters = max_line;
      unsigned lines = len / max_line;

      if (lines > 0) {
        for (; characters > max_token; --characters)
          if (len / characters > lines) {
            ++characters;
            break;
          }
      }

      em = characters / 2;
    }

    if (em < max_line/2)
      os << "; max-width:" << em << "em";
  }
  else
    os << "; max-width:100em";

  os << "\">";

  if (max > 1) {
    os << "<table class=\"msgT\"><tr><td valign=\"top\">";
    os << "<div class=\"PathIndex";
    if (Kind) os << " PathIndex" << Kind;
    os << "\">" << num << "</div>";

    if (num > 1) {
      os << "</td><td><div class=\"PathNav\"><a href=\"#Path"
         << (num - 1)
         << "\" title=\"Previous event ("
         << (num - 1)
         << ")\">&#x2190;</a></div></td>";
    }

    os << "</td><td>";
  }

  if (const PathDiagnosticMacroPiece *MP =
        dyn_cast<PathDiagnosticMacroPiece>(&P)) {

    os << "Within the expansion of the macro '";

    // Get the name of the macro by relexing it.
    {
      FullSourceLoc L = MP->getLocation().asLocation().getExpansionLoc();
      assert(L.isFileID());
      StringRef BufferInfo = L.getBufferData();
      std::pair<FileID, unsigned> LocInfo = L.getDecomposedLoc();
      const char* MacroName = LocInfo.second + BufferInfo.data();
      Lexer rawLexer(SM.getLocForStartOfFile(LocInfo.first), PP.getLangOpts(),
                     BufferInfo.begin(), MacroName, BufferInfo.end());

      Token TheTok;
      rawLexer.LexFromRawLexer(TheTok);
      for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
        os << MacroName[i];
    }

    os << "':\n";

    if (max > 1) {
      os << "</td>";
      if (num < max) {
        os << "<td><div class=\"PathNav\"><a href=\"#";
        if (num == max - 1)
          os << "EndPath";
        else
          os << "Path" << (num + 1);
        os << "\" title=\"Next event ("
        << (num + 1)
        << ")\">&#x2192;</a></div></td>";
      }

      os << "</tr></table>";
    }

    // Within a macro piece.  Write out each event.
    ProcessMacroPiece(os, *MP, 0);
  }
  else {
    os << html::EscapeText(P.getString());

    if (max > 1) {
      os << "</td>";
      if (num < max) {
        os << "<td><div class=\"PathNav\"><a href=\"#";
        if (num == max - 1)
          os << "EndPath";
        else
          os << "Path" << (num + 1);
        os << "\" title=\"Next event ("
           << (num + 1)
           << ")\">&#x2192;</a></div></td>";
      }
      
      os << "</tr></table>";
    }
  }

  os << "</div></td></tr>";

  // Insert the new html.
  unsigned DisplayPos = LineEnd - FileStart;
  SourceLocation Loc =
    SM.getLocForStartOfFile(LPosInfo.first).getLocWithOffset(DisplayPos);

  R.InsertTextBefore(Loc, os.str());

  // Now highlight the ranges.
  ArrayRef<SourceRange> Ranges = P.getRanges();
  for (ArrayRef<SourceRange>::iterator I = Ranges.begin(),
                                       E = Ranges.end(); I != E; ++I) {
    HighlightRange(R, LPosInfo.first, *I);
  }
}
コード例 #7
0
ファイル: Diagnostic.cpp プロジェクト: degano/clang
void DiagnosticsEngine::setSeverity(diag::kind Diag, diag::Severity Map,
                                    SourceLocation L) {
    assert(Diag < diag::DIAG_UPPER_LIMIT &&
           "Can only map builtin diagnostics");
    assert((Diags->isBuiltinWarningOrExtension(Diag) ||
            (Map == diag::Severity::Fatal || Map == diag::Severity::Error)) &&
           "Cannot map errors into warnings!");
    assert(!DiagStatePoints.empty());
    assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");

    FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc();
    FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
    // Don't allow a mapping to a warning override an error/fatal mapping.
    if (Map == diag::Severity::Warning) {
        DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
        if (Info.getSeverity() == diag::Severity::Error ||
                Info.getSeverity() == diag::Severity::Fatal)
            Map = Info.getSeverity();
    }
    DiagnosticMapping Mapping = makeUserMapping(Map, L);

    // Common case; setting all the diagnostics of a group in one place.
    if (Loc.isInvalid() || Loc == LastStateChangePos) {
        GetCurDiagState()->setMapping(Diag, Mapping);
        return;
    }

    // Another common case; modifying diagnostic state in a source location
    // after the previous one.
    if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
            LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
        // A diagnostic pragma occurred, create a new DiagState initialized with
        // the current one and a new DiagStatePoint to record at which location
        // the new state became active.
        DiagStates.push_back(*GetCurDiagState());
        PushDiagStatePoint(&DiagStates.back(), Loc);
        GetCurDiagState()->setMapping(Diag, Mapping);
        return;
    }

    // We allow setting the diagnostic state in random source order for
    // completeness but it should not be actually happening in normal practice.

    DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
    assert(Pos != DiagStatePoints.end());

    // Update all diagnostic states that are active after the given location.
    for (DiagStatePointsTy::iterator
            I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
        GetCurDiagState()->setMapping(Diag, Mapping);
    }

    // If the location corresponds to an existing point, just update its state.
    if (Pos->Loc == Loc) {
        GetCurDiagState()->setMapping(Diag, Mapping);
        return;
    }

    // Create a new state/point and fit it into the vector of DiagStatePoints
    // so that the vector is always ordered according to location.
    assert(Pos->Loc.isBeforeInTranslationUnitThan(Loc));
    DiagStates.push_back(*Pos->State);
    DiagState *NewState = &DiagStates.back();
    GetCurDiagState()->setMapping(Diag, Mapping);
    DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
                           FullSourceLoc(Loc, *SourceMgr)));
}
コード例 #8
0
ファイル: TextDiagnostic.cpp プロジェクト: Teemperor/clang
/// Emit a code snippet and caret line.
///
/// This routine emits a single line's code snippet and caret line..
///
/// \param Loc The location for the caret.
/// \param Ranges The underlined ranges for this code snippet.
/// \param Hints The FixIt hints active for this diagnostic.
void TextDiagnostic::emitSnippetAndCaret(
    FullSourceLoc Loc, DiagnosticsEngine::Level Level,
    SmallVectorImpl<CharSourceRange> &Ranges, ArrayRef<FixItHint> Hints) {
  assert(Loc.isValid() && "must have a valid source location here");
  assert(Loc.isFileID() && "must have a file location here");

  // If caret diagnostics are enabled and we have location, we want to
  // emit the caret.  However, we only do this if the location moved
  // from the last diagnostic, if the last diagnostic was a note that
  // was part of a different warning or error diagnostic, or if the
  // diagnostic has ranges.  We don't want to emit the same caret
  // multiple times if one loc has multiple diagnostics.
  if (!DiagOpts->ShowCarets)
    return;
  if (Loc == LastLoc && Ranges.empty() && Hints.empty() &&
      (LastLevel != DiagnosticsEngine::Note || Level == LastLevel))
    return;

  // Decompose the location into a FID/Offset pair.
  std::pair<FileID, unsigned> LocInfo = Loc.getDecomposedLoc();
  FileID FID = LocInfo.first;
  const SourceManager &SM = Loc.getManager();

  // Get information about the buffer it points into.
  bool Invalid = false;
  StringRef BufData = Loc.getBufferData(&Invalid);
  if (Invalid)
    return;

  unsigned CaretLineNo = Loc.getLineNumber();
  unsigned CaretColNo = Loc.getColumnNumber();

  // Arbitrarily stop showing snippets when the line is too long.
  static const size_t MaxLineLengthToPrint = 4096;
  if (CaretColNo > MaxLineLengthToPrint)
    return;

  // Find the set of lines to include.
  const unsigned MaxLines = DiagOpts->SnippetLineLimit;
  std::pair<unsigned, unsigned> Lines = {CaretLineNo, CaretLineNo};
  for (SmallVectorImpl<CharSourceRange>::iterator I = Ranges.begin(),
                                                  E = Ranges.end();
       I != E; ++I)
    if (auto OptionalRange = findLinesForRange(*I, FID, SM))
      Lines = maybeAddRange(Lines, *OptionalRange, MaxLines);

  for (unsigned LineNo = Lines.first; LineNo != Lines.second + 1; ++LineNo) {
    const char *BufStart = BufData.data();
    const char *BufEnd = BufStart + BufData.size();

    // Rewind from the current position to the start of the line.
    const char *LineStart =
        BufStart +
        SM.getDecomposedLoc(SM.translateLineCol(FID, LineNo, 1)).second;
    if (LineStart == BufEnd)
      break;

    // Compute the line end.
    const char *LineEnd = LineStart;
    while (*LineEnd != '\n' && *LineEnd != '\r' && LineEnd != BufEnd)
      ++LineEnd;

    // Arbitrarily stop showing snippets when the line is too long.
    // FIXME: Don't print any lines in this case.
    if (size_t(LineEnd - LineStart) > MaxLineLengthToPrint)
      return;

    // Trim trailing null-bytes.
    StringRef Line(LineStart, LineEnd - LineStart);
    while (!Line.empty() && Line.back() == '\0' &&
           (LineNo != CaretLineNo || Line.size() > CaretColNo))
      Line = Line.drop_back();

    // Copy the line of code into an std::string for ease of manipulation.
    std::string SourceLine(Line.begin(), Line.end());

    // Build the byte to column map.
    const SourceColumnMap sourceColMap(SourceLine, DiagOpts->TabStop);

    // Create a line for the caret that is filled with spaces that is the same
    // number of columns as the line of source code.
    std::string CaretLine(sourceColMap.columns(), ' ');

    // Highlight all of the characters covered by Ranges with ~ characters.
    for (SmallVectorImpl<CharSourceRange>::iterator I = Ranges.begin(),
                                                    E = Ranges.end();
         I != E; ++I)
      highlightRange(*I, LineNo, FID, sourceColMap, CaretLine, SM, LangOpts);

    // Next, insert the caret itself.
    if (CaretLineNo == LineNo) {
      CaretColNo = sourceColMap.byteToContainingColumn(CaretColNo - 1);
      if (CaretLine.size() < CaretColNo + 1)
        CaretLine.resize(CaretColNo + 1, ' ');
      CaretLine[CaretColNo] = '^';
    }

    std::string FixItInsertionLine = buildFixItInsertionLine(
        FID, LineNo, sourceColMap, Hints, SM, DiagOpts.get());

    // If the source line is too long for our terminal, select only the
    // "interesting" source region within that line.
    unsigned Columns = DiagOpts->MessageLength;
    if (Columns)
      selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine,
                                    Columns, sourceColMap);

    // If we are in -fdiagnostics-print-source-range-info mode, we are trying
    // to produce easily machine parsable output.  Add a space before the
    // source line and the caret to make it trivial to tell the main diagnostic
    // line from what the user is intended to see.
    if (DiagOpts->ShowSourceRanges) {
      SourceLine = ' ' + SourceLine;
      CaretLine = ' ' + CaretLine;
    }

    // Finally, remove any blank spaces from the end of CaretLine.
    while (!CaretLine.empty() && CaretLine[CaretLine.size() - 1] == ' ')
      CaretLine.erase(CaretLine.end() - 1);

    // Emit what we have computed.
    emitSnippet(SourceLine);

    if (!CaretLine.empty()) {
      if (DiagOpts->ShowColors)
        OS.changeColor(caretColor, true);
      OS << CaretLine << '\n';
      if (DiagOpts->ShowColors)
        OS.resetColor();
    }

    if (!FixItInsertionLine.empty()) {
      if (DiagOpts->ShowColors)
        // Print fixit line in color
        OS.changeColor(fixitColor, false);
      if (DiagOpts->ShowSourceRanges)
        OS << ' ';
      OS << FixItInsertionLine << '\n';
      if (DiagOpts->ShowColors)
        OS.resetColor();
    }
  }

  // Print out any parseable fixit information requested by the options.
  emitParseableFixits(Hints, SM);
}
コード例 #9
0
ファイル: llvm_codes.cpp プロジェクト: Goettsch/game-editor
void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic &Diags,
                                             Diagnostic::Level Level, 
                                             FullSourceLoc Pos,
                                             diag::kind ID,
                                             const std::string *Strs,
                                             unsigned NumStrs,
                                             const SourceRange *Ranges,
                                             unsigned NumRanges) {
  unsigned LineNo = 0, ColNo = 0;
  const char *LineStart = 0, *LineEnd = 0;
  
  if (Pos.isValid()) {
    FullSourceLoc LPos = Pos.getLogicalLoc();
    LineNo = LPos.getLineNumber();
    
    // First, if this diagnostic is not in the main file, print out the
    // "included from" lines.
    if (LastWarningLoc != LPos.getIncludeLoc()) {
      LastWarningLoc = LPos.getIncludeLoc();
      PrintIncludeStack(LastWarningLoc);
    }
  
    // Compute the column number.  Rewind from the current position to the start
    // of the line.
    ColNo = LPos.getColumnNumber();
    const char *TokLogicalPtr = LPos.getCharacterData();
    LineStart = TokLogicalPtr-ColNo+1;  // Column # is 1-based
  
    // Compute the line end.  Scan forward from the error position to the end of
    // the line.
    const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
    const char *BufEnd = Buffer->getBufferEnd();
    LineEnd = TokLogicalPtr;
    while (LineEnd != BufEnd && 
           *LineEnd != '\n' && *LineEnd != '\r')
      ++LineEnd;
  
    std::cerr << Buffer->getBufferIdentifier() 
              << ":" << LineNo << ":";
    if (ColNo && !NoShowColumn) 
      std::cerr << ColNo << ":";
    std::cerr << " ";
  }
  
  switch (Level) {
  default: assert(0 && "Unknown diagnostic type!");
  case Diagnostic::Note:    std::cerr << "note: "; break;
  case Diagnostic::Warning: std::cerr << "warning: "; break;
  case Diagnostic::Error:   std::cerr << "error: "; break;
  case Diagnostic::Fatal:   std::cerr << "fatal error: "; break;
    break;
  }
  
  std::cerr << FormatDiagnostic(Diags, Level, ID, Strs, NumStrs) << "\n";
  
  if (!NoCaretDiagnostics && Pos.isValid()) {
    // Get the line of the source file.
    std::string SourceLine(LineStart, LineEnd);
    
    // Create a line for the carat that is filled with spaces that is the same
    // length as the line of source code.
    std::string CaratLine(LineEnd-LineStart, ' ');
    
    // Highlight all of the characters covered by Ranges with ~ characters.
    for (unsigned i = 0; i != NumRanges; ++i)
      HighlightRange(Ranges[i], Pos.getManager(),LineNo, CaratLine, SourceLine);
    
    // Next, insert the carat itself.
    if (ColNo-1 < CaratLine.size())
      CaratLine[ColNo-1] = '^';
    else
      CaratLine.push_back('^');
    
    // Scan the source line, looking for tabs.  If we find any, manually expand
    // them to 8 characters and update the CaratLine to match.
    for (unsigned i = 0; i != SourceLine.size(); ++i) {
      if (SourceLine[i] != '\t') continue;
      
      // Replace this tab with at least one space.
      SourceLine[i] = ' ';
      
      // Compute the number of spaces we need to insert.
      unsigned NumSpaces = ((i+8)&~7) - (i+1);
      assert(NumSpaces < 8 && "Invalid computation of space amt");
      
      // Insert spaces into the SourceLine.
      SourceLine.insert(i+1, NumSpaces, ' ');
      
      // Insert spaces or ~'s into CaratLine.
      CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' ');
    }
    
    // Finally, remove any blank spaces from the end of CaratLine.
    while (CaratLine[CaratLine.size()-1] == ' ')
      CaratLine.erase(CaratLine.end()-1);
    
    // Emit what we have computed.
    std::cerr << SourceLine << "\n";
    std::cerr << CaratLine << "\n";
  }
}
コード例 #10
0
ファイル: stylecheck.cpp プロジェクト: 151706061/sofa
    // http://clang.llvm.org/doxygen/classclang_1_1Stmt.html
    // For each declaration
    // http://clang.llvm.org/doxygen/classclang_1_1Decl.html
    // http://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html
    // and
    // http://clang.llvm.org/doxygen/classclang_1_1RecursiveASTVisitor.html
    bool VisitCXXRecordDecl(CXXRecordDecl *record) {
        if(Context==NULL)
            return true ;

        if(record==NULL)
            return true ;

        auto& smanager = Context->getSourceManager() ;

        FullSourceLoc FullLocation = Context->getFullLoc(record->getLocStart());
        // Check this declaration is not in the system headers...
        if ( FullLocation.isValid() && !exclude(FullLocation.getManager() , record) )
        {

            // Check the class name.
            // it should be in writtent in UpperCamlCase
            string classname=record->getNameAsString();

            if(!isUpperCamlCase(classname)){
                SourceRange declsr=record->getMostRecentDecl()->getSourceRange() ;
                SourceLocation sl=declsr.getBegin();

                auto fileinfo = smanager.getFileEntryForID(smanager.getFileID(sl)) ;

                if(fileinfo && !isInExcludedPath(fileinfo->getName(), excludedPathPatterns)){
                    printErrorM4(fileinfo->getName(),
                                 smanager.getPresumedLineNumber(sl),
                                 smanager.getPresumedColumnNumber(sl),
                                 classname) ;

                }
            }

            // Check the function definitions
            //
            if(qualityLevel>=Q2){
                for(auto f=record->method_begin();f!=record->method_end();++f){

                    SourceRange declsr=(*f)->getSourceRange() ;
                    SourceLocation sl=declsr.getBegin();
                    auto fileinfo = smanager.getFileEntryForID(smanager.getFileID(sl)) ;

                    if(fileinfo!=NULL && !isInExcludedPath(fileinfo->getName(), excludedPathPatterns))
                    {
                        // Rules C1: check that a function definition is not in a body.
                        if((*f)->hasBody())
                        {
                            Stmt* body=(*f)->getBody();

                            SourceRange bodysr=body->getSourceRange() ;
                            SourceLocation bodysl=bodysr.getBegin();
                            SourceLocation bodyend=bodysr.getEnd();
                            auto fileinfobody = smanager.getFileEntryForID(smanager.getFileID(bodysl)) ;

                            if(fileinfobody
                                    && isInHeader(fileinfobody->getName())
                                    && smanager.getPresumedLineNumber(bodyend)- smanager.getPresumedLineNumber(bodysl) > 1 ){
                               printErrorC1(fileinfo->getName(), smanager.getPresumedLineNumber(sl), smanager.getPresumedColumnNumber(sl),
                                           record->getNameAsString(), f->getNameAsString());
                            }
                        }

                        // Rules C2: check that a method name is following a LowerCamlCase mode
                        if(!isLowerCamlCase(f->getNameAsString())
                           && !f->isCopyAssignmentOperator()
                           && !f->isMoveAssignmentOperator()
                           && !CXXConstructorDecl::classof(*f)
                           && !CXXDestructorDecl::classof(*f)
                           && !f->isOverloadedOperator())
                        {
                            printErrorC2(fileinfo->getName(), smanager.getPresumedLineNumber(sl), smanager.getPresumedColumnNumber(sl),
                                         record->getNameAsString(), f->getNameAsString());
                        }

                        // Rules M3: check that all ExecParam are the first param and that they have no default arg.
                        for(auto p=(*f)->param_begin();p!=(*f)->param_end();++p)
                        {
                            string fullname=(*p)->getOriginalType().getCanonicalType().getAsString() ;
                            if(isAnExecParam(fullname)){
                                if( (*p)->hasUnparsedDefaultArg() ||
                                    (*p)->hasDefaultArg() ){
                                    printErrorW4(fileinfo->getName(), smanager.getPresumedLineNumber(sl), smanager.getPresumedColumnNumber(sl),
                                                 fullname, (*p)->getName());
                                }
                                if( p != (*f)->param_begin() ){
                                    printErrorW5(fileinfo->getName(), smanager.getPresumedLineNumber(sl), smanager.getPresumedColumnNumber(sl),
                                                 fullname, (*p)->getName());
                                }

                            }
                        }
                    }
                }
            }

            // Now check the attributes...
            RecordDecl::field_iterator it=record->field_begin() ;
            for(;it!=record->field_end();it++){
                clang::FieldDecl* ff=*it;

                SourceRange declsr=ff->getMostRecentDecl()->getSourceRange() ;
                SourceLocation sl=declsr.getBegin();
                std::string name=ff->getName() ;

                auto fileinfo = smanager.getFileEntryForID(smanager.getFileID(sl)) ;

                if( fileinfo == NULL ){
                    continue ;
                }

                if(isInExcludedPath(fileinfo->getName(), excludedPathPatterns)){
                    continue ;
                }

                if(name.size()==0){
                    continue ;
                }

                const std::string filename=fileinfo->getName() ;
                const int line = smanager.getPresumedLineNumber(sl) ;
                const int col = smanager.getPresumedColumnNumber(sl) ;

                // RULES NUMBER 1: The name of members cannot be terminated by an underscore.
                if(name.rfind("_")!=name.size()-1){
                }else{
                    printErrorM5(filename, line, col, classname, name);
                }

                /// THESES TWO RULES ARE NOW DEPRECATED BUT I KEEP THEM FOR HISTORY REASON
                // THE FOLLOWING RULES ARE ONLY CHECK ON PRIVATE & PROTECTED FIELDS
                if(ff->getAccess()==AS_public){
                    continue ;
                }

                CXXRecordDecl* rd=ff->getType()->getAsCXXRecordDecl() ;
                if(rd){
                    std::string type=rd->getNameAsString() ;
                    if(type.find("Data")!=std::string::npos){
                        if(name.find("d_")==0){
                        }else{
                            printErrorM1(filename, line, col,
                                         classname, name) ;

                        }
                    }else if(type.find("SingleLink")!=std::string::npos || type.find("DualLink")!=std::string::npos){
                        if(name.find("d_")==0){
                        }else{
                            printErrorM2(filename, line, col, classname, name) ;
                        }
                    }
                }else{
                    if(name.find("m_")==0){
                    }else{
                        printErrorM3(filename, line, col, classname, name) ;
                    }
                }
            }
        }
        return true;
    }
コード例 #11
0
ファイル: stylecheck.cpp プロジェクト: 151706061/sofa
    bool VisitDecl(Decl* decl)
    {
        if(Context==NULL)
            return true ;

        if(decl==NULL)
            return true ;

        FullSourceLoc FullLocation = Context->getFullLoc(decl->getLocStart());
        if ( !FullLocation.isValid() || exclude(FullLocation.getManager() , decl) )
            return true ;

        /// Implement the different check on namespace naming.
        NamespaceDecl* nsdecl= dyn_cast<NamespaceDecl>(decl) ;
        if( nsdecl ){
            string nsname=nsdecl->getNameAsString() ;
            if( islower(nsname) )
                return true ;

            auto& smanager = Context->getSourceManager() ;

            Decl* mrdecl=decl->getMostRecentDecl() ;
            if(mrdecl!=NULL)
                decl=mrdecl ;

            SourceRange sr=decl->getSourceRange() ;
            SourceLocation sl=sr.getBegin();
            auto fileinfo=smanager.getFileEntryForID(smanager.getFileID(sl)) ;


            if(fileinfo==NULL || isInExcludedPath(fileinfo->getName(), excludedPathPatterns))
                return true ;

            printErrorN1(fileinfo->getName(),
                         smanager.getPresumedLineNumber(sl),
                         smanager.getPresumedColumnNumber(sl),
                         nsname) ;
            return true;
        }

        UsingDirectiveDecl* udecl = dyn_cast<UsingDirectiveDecl>(decl) ;
        if(udecl){
             auto& smanager = Context->getSourceManager() ;
             SourceRange sr=decl->getSourceRange() ;
             SourceLocation sl=sr.getBegin();
             auto fileinfo=smanager.getFileEntryForID(smanager.getFileID(sl)) ;
             string nsname = udecl->getNominatedNamespaceAsWritten()->getName() ;

             if(fileinfo==NULL || isInExcludedPath(fileinfo->getName(), excludedPathPatterns))
                 return true ;

             if(isInHeader(fileinfo->getName()) ){
                printErrorW3(fileinfo->getName(),
                             smanager.getPresumedLineNumber(sl),
                             smanager.getPresumedColumnNumber(sl),
                             nsname);
             }
             return true ;

        }

        return RecursiveASTVisitor<StyleChecker>::VisitDecl(decl) ;
    }
コード例 #12
0
ファイル: stylecheck.cpp プロジェクト: 151706061/sofa
    bool VisitStmt(Stmt* stmt){
        if(Context == NULL )
            return true ;

        if( stmt == NULL )
            return true ;

        FullSourceLoc FullLocation = Context->getFullLoc(stmt->getLocStart()) ;
        if ( !FullLocation.isValid() || exclude(FullLocation.getManager() , stmt) )
            return true ;

        // If we are on a declaration statement, check that we
        // correctly provide a default value.
        if(stmt->getStmtClass() == Stmt::DeclStmtClass) {
            auto& smanager=Context->getSourceManager() ;

            DeclStmt* declstmt=dyn_cast<DeclStmt>(stmt) ;
            for(auto cs=declstmt->decl_begin(); cs!=declstmt->decl_end();++cs) {
                Decl* decl = *cs;
                VarDecl* vardecl = dyn_cast<VarDecl>(decl) ;

                if(vardecl){
                    auto tmp=vardecl->getMostRecentDecl() ;
                    if(tmp)
                        decl=tmp ;

                    SourceRange declsr=decl->getSourceRange() ;
                    SourceLocation sl=declsr.getBegin();

                    auto fileinfo=smanager.getFileEntryForID(smanager.getFileID(sl)) ;

                    if(fileinfo==NULL || isInExcludedPath(fileinfo->getName(), excludedPathPatterns))
                        continue ;

                    if( vardecl->getAnyInitializer() == NULL ){
                        printErrorV1(fileinfo->getName(),
                                     smanager.getPresumedLineNumber(sl),
                                     smanager.getPresumedColumnNumber(sl),
                                     vardecl->getNameAsString()) ;

                    }
                }
            }

        }else if(stmt->getStmtClass() == Stmt::GotoStmtClass){
            SourceRange sr=stmt->getSourceRange() ;
            SourceLocation sl=sr.getBegin() ;
            auto& smanager=Context->getSourceManager() ;
            auto fileinfo=smanager.getFileEntryForID(smanager.getFileID(sl)) ;

            if(fileinfo==NULL || isInExcludedPath(fileinfo->getName(), excludedPathPatterns))
                return true ;

            printErrorW1(fileinfo->getName(),
                         smanager.getPresumedLineNumber(sl),
                         smanager.getPresumedColumnNumber(sl));
        }else if(stmt->getStmtClass() == Stmt::CallExprClass){
            CallExpr* callexpr=dyn_cast<CallExpr>(stmt) ;
            FunctionDecl* fctdecl=callexpr->getDirectCallee() ;
            if(fctdecl){
                const string& fctname = fctdecl->getNameAsString() ;
                for(auto p : oldccode)
                {
                    if(fctname == p.first){
                        SourceRange sr=stmt->getSourceRange() ;
                        SourceLocation sl=sr.getBegin() ;
                        auto& smanager=Context->getSourceManager() ;
                        auto fileinfo=smanager.getFileEntryForID(smanager.getFileID(sl)) ;

                        if(fileinfo==NULL || isInExcludedPath(fileinfo->getName(), excludedPathPatterns))
                            return true ;

                        printErrorW2(fileinfo->getName(),
                                     smanager.getPresumedLineNumber(sl),
                                     smanager.getPresumedColumnNumber(sl),
                                     p.first, p.second);
                        break ;
                    }
                }
            }
        }

        return true ;
    }