///get the source code of stmt
StringRef FindPatternVisitor::expr2str(Stmt *s){
    
    if(!s)
        return "";

    FullSourceLoc begin = CI->getASTContext().getFullLoc(s->getLocStart());
    FullSourceLoc end = CI->getASTContext().getFullLoc(s->getLocEnd());
    
    if(begin.isInvalid() || end.isInvalid())
        return "";
    
    SourceRange sr(begin.getExpansionLoc(), end.getExpansionLoc());
    
    return Lexer::getSourceText(CharSourceRange::getTokenRange(sr), CI->getSourceManager(), LangOptions(), 0);
}
Exemple #2
0
/// \brief This allows the client to specify that certain
/// warnings are ignored.  Notes can never be mapped, errors can only be
/// mapped to fatal, and WARNINGs and EXTENSIONs can be mapped arbitrarily.
///
/// \param The source location that this change of diagnostic state should
/// take affect. It can be null if we are setting the latest state.
void Diagnostic::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
                                      SourceLocation L) {
    assert(Diag < diag::DIAG_UPPER_LIMIT &&
           "Can only map builtin diagnostics");
    assert((Diags->isBuiltinWarningOrExtension(Diag) ||
            (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
           "Cannot map errors into warnings!");
    assert(!DiagStatePoints.empty());

    bool isPragma = L.isValid();
    FullSourceLoc Loc(L, *SourceMgr);
    FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;

    // Common case; setting all the diagnostics of a group in one place.
    if (Loc.isInvalid() || Loc == LastStateChangePos) {
        setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma);
        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);
        setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma);
        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) {
        setDiagnosticMappingInternal(Diag, Map, I->State, true, isPragma);
    }

    // If the location corresponds to an existing point, just update its state.
    if (Pos->Loc == Loc) {
        setDiagnosticMappingInternal(Diag, Map, Pos->State, true, isPragma);
        return;
    }

    // Create a new state/point and fit it into the vector of DiagStatePoints
    // so that the vector is always ordered according to location.
    Pos->Loc.isBeforeInTranslationUnitThan(Loc);
    DiagStates.push_back(*Pos->State);
    DiagState *NewState = &DiagStates.back();
    setDiagnosticMappingInternal(Diag, Map, NewState, true, isPragma);
    DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
                           FullSourceLoc(Loc, *SourceMgr)));
}
Exemple #3
0
void TextDiagnosticPrinter::
PrintIncludeStack(FullSourceLoc Pos) {
  if (Pos.isInvalid()) return;

  Pos = Pos.getLogicalLoc();

  // Print out the other include frames first.
  PrintIncludeStack(Pos.getIncludeLoc());
  unsigned LineNo = Pos.getLineNumber();
  
  std::cerr << "In file included from " << Pos.getSourceName()
            << ":" << LineNo << ":\n";
}
Exemple #4
0
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)));
}