예제 #1
0
파일: main.cpp 프로젝트: qyqx/moc-ng
    void HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic& Info) override {

        /* Moc ignores most of the errors since it even can operate on non self-contained headers.
         * So try to change errors into warning.
         */

        auto DiagId = Info.getID();
        auto Cat = Info.getDiags()->getDiagnosticIDs()->getCategoryNumberForDiag(DiagId);

        bool ShouldReset = false;

        if (DiagLevel >= clang::DiagnosticsEngine::Error ) {
            if (Cat == 2 || Cat == 4
                || DiagId == clang::diag::err_param_redefinition
                || DiagId == clang::diag::err_pp_expr_bad_token_binop ) {
                if (!HadRealError)
                    ShouldReset = true;
                DiagLevel = clang::DiagnosticsEngine::Warning;
            } else {
                HadRealError++;
            }
        }

        DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
        Proxy->HandleDiagnostic(DiagLevel, Info);

        if (ShouldReset) {
            // FIXME:  is there another way to ignore errors?
            const_cast<clang::DiagnosticsEngine *>(Info.getDiags())->Reset();
        }
    }
예제 #2
0
void DiagnosticDispatcher::HandleDiagnostic(clang::DiagnosticsEngine::Level diagnosticLevel,
    const clang::Diagnostic &diagnosticInfo)
{
    clang::DiagnosticConsumer::HandleDiagnostic(diagnosticLevel, diagnosticInfo);

    clang::SourceLocation location = diagnosticInfo.getLocation();
    clang::SourceManager *sourceManager = &diagnosticInfo.getSourceManager();
    llvm::StringRef filename = sourceManager->getFilename(location);
    int line = sourceManager->getPresumedLineNumber(location);
    int column = sourceManager->getPresumedColumnNumber(location);

    clang::SmallString<100> diagnosticMessage;
    diagnosticInfo.FormatDiagnostic(diagnosticMessage);

    Violation violation(nullptr, filename.str(), line, column, 0, 0,
                        diagnosticMessage.str().str());

    Results *results = Results::getInstance();
    if (_isCheckerCustomer)
    {
        results->addCheckerBug(violation);
    }
    else
    {
        if (diagnosticLevel == clang::DiagnosticsEngine::Warning)
        {
            results->addWarning(violation);
        }
        if (diagnosticLevel == clang::DiagnosticsEngine::Error ||
            diagnosticLevel == clang::DiagnosticsEngine::Fatal)
        {
            results->addError(violation);
        }
    }
}
예제 #3
0
    void
    HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info)
    {
        if (m_manager)
        {
            llvm::SmallVector<char, 32> diag_str;
            Info.FormatDiagnostic(diag_str);
            diag_str.push_back('\0');
            const char *data = diag_str.data();

            lldb_private::DiagnosticSeverity severity;
            bool make_new_diagnostic = true;
            
            switch (DiagLevel)
            {
                case DiagnosticsEngine::Level::Fatal:
                case DiagnosticsEngine::Level::Error:
                    severity = eDiagnosticSeverityError;
                    break;
                case DiagnosticsEngine::Level::Warning:
                    severity = eDiagnosticSeverityWarning;
                    break;
                case DiagnosticsEngine::Level::Remark:
                case DiagnosticsEngine::Level::Ignored:
                    severity = eDiagnosticSeverityRemark;
                    break;
                case DiagnosticsEngine::Level::Note:
                    m_manager->AppendMessageToDiagnostic(data);
                    make_new_diagnostic = false;
            }
            if (make_new_diagnostic)
            {
                ClangDiagnostic *new_diagnostic = new ClangDiagnostic(data, severity, Info.getID());
                m_manager->AddDiagnostic(new_diagnostic);
                
                // Don't store away warning fixits, since the compiler doesn't have enough
                // context in an expression for the warning to be useful.
                // FIXME: Should we try to filter out FixIts that apply to our generated
                // code, and not the user's expression?
                if (severity == eDiagnosticSeverityError)
                {
                    size_t num_fixit_hints = Info.getNumFixItHints();
                    for (size_t i = 0; i < num_fixit_hints; i++)
                    {
                        const clang::FixItHint &fixit = Info.getFixItHint(i);
                        if (!fixit.isNull())
                            new_diagnostic->AddFixitHint(fixit);
                    }
                }
            }
        }
        
        m_passthrough->HandleDiagnostic(DiagLevel, Info);
    }
예제 #4
0
    void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info)
    {
        if (m_log)
        {
            llvm::SmallVector<char, 32> diag_str;
            Info.FormatDiagnostic(diag_str);
            diag_str.push_back('\0');
            const char *data = diag_str.data();
            m_log->Printf("[clang] COMPILER DIAGNOSTIC: %s", data);

            lldbassert(Info.getID() != clang::diag::err_unsupported_ast_node && "'log enable lldb expr' to investigate.");
        }
        
        m_passthrough->HandleDiagnostic(DiagLevel, Info);
    }
예제 #5
0
void IgnoreDiagnostics::log(DiagnosticsEngine::Level DiagLevel,
                            const clang::Diagnostic &Info) {
  SmallString<64> Message;
  Info.FormatDiagnostic(Message);

  SmallString<64> Location;
  if (Info.hasSourceManager() && Info.getLocation().isValid()) {
    auto &SourceMgr = Info.getSourceManager();
    auto Loc = SourceMgr.getFileLoc(Info.getLocation());
    llvm::raw_svector_ostream OS(Location);
    Loc.print(OS, SourceMgr);
    OS << ":";
  }

  clangd::log(llvm::formatv("Ignored diagnostic. {0}{1}", Location, Message));
}
예제 #6
0
void
StoringDiagnosticConsumer::HandleDiagnostic (clang::DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &info)
{
    llvm::SmallVector<char, 256> diagnostic_string;
    
    info.FormatDiagnostic(diagnostic_string);
    
    m_diagnostics.push_back(IDAndDiagnostic(DiagLevel, std::string(diagnostic_string.data(), diagnostic_string.size())));
}
예제 #7
0
int ArgList::getLastArgIntValue(OptSpecifier Id, int Default,
                                clang::Diagnostic &Diags) const {
  int Res = Default;

  if (Arg *A = getLastArg(Id)) {
    if (llvm::StringRef(A->getValue(*this)).getAsInteger(10, Res))
      Diags.Report(diag::err_drv_invalid_int_value)
        << A->getAsString(*this) << A->getValue(*this);
  }

  return Res;
}
예제 #8
0
    virtual void HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic& Info) override {
        std::string clas;
        llvm::SmallString<1000> diag;
        Info.FormatDiagnostic(diag);

        switch(DiagLevel) {
            case clang::DiagnosticsEngine::Fatal:
                std::cerr << "FATAL ";
            case clang::DiagnosticsEngine::Error:
                std::cerr << "Error: " << locationToString(Info.getLocation(), annotator.getSourceMgr())
                            << ": " << diag.c_str() << std::endl;
                clas = "error";
                break;
            case clang::DiagnosticsEngine::Warning:
                clas = "warning";
                break;
            default:
                return;
        }
        annotator.reportDiagnostic(Info.getLocation(), diag.c_str(), clas);
    }
// ParseArguments -
static void ParseArguments(llvm::SmallVectorImpl<const char*> &ArgVector,
                           llvm::SmallVectorImpl<const char*> &Inputs,
                           RSCCOptions &Opts,
                           clang::Diagnostic &Diags) {
    if (ArgVector.size() > 1) {
        const char **ArgBegin = ArgVector.data() + 1;
        const char **ArgEnd = ArgVector.data() + ArgVector.size();
        unsigned MissingArgIndex, MissingArgCount;
        llvm::OwningPtr<OptTable> OptParser(createRSCCOptTable());
        llvm::OwningPtr<InputArgList> Args(
            OptParser->ParseArgs(ArgBegin, ArgEnd, MissingArgIndex, MissingArgCount));

        // Check for missing argument error.
        if (MissingArgCount)
            Diags.Report(clang::diag::err_drv_missing_argument)
                    << Args->getArgString(MissingArgIndex) << MissingArgCount;

        // Issue errors on unknown arguments.
        for (arg_iterator it = Args->filtered_begin(OPT_UNKNOWN),
                ie = Args->filtered_end(); it != ie; ++it)
            Diags.Report(clang::diag::err_drv_unknown_argument)
                    << (*it)->getAsString(*Args);

        for (ArgList::const_iterator it = Args->begin(), ie = Args->end();
                it != ie; ++it) {
            const Arg *A = *it;
            if (A->getOption().getKind() == Option::InputClass)
                Inputs.push_back(A->getValue(*Args));
        }

        Opts.mIncludePaths = Args->getAllArgValues(OPT_I);

        Opts.mOutputDir = Args->getLastArgValue(OPT_o);

        if (const Arg *A = Args->getLastArg(OPT_M_Group)) {
            switch (A->getOption().getID()) {
            case OPT_M: {
                Opts.mOutputDep = 1;
                Opts.mOutputType = slang::Slang::OT_Dependency;
                break;
            }
            case OPT_MD: {
                Opts.mOutputDep = 1;
                Opts.mOutputType = slang::Slang::OT_Bitcode;
                break;
            }
            default: {
                slangAssert(false && "Invalid option in M group!");
            }
            }
        }

        if (const Arg *A = Args->getLastArg(OPT_Output_Type_Group)) {
            switch (A->getOption().getID()) {
            case OPT_emit_asm: {
                Opts.mOutputType = slang::Slang::OT_Assembly;
                break;
            }
            case OPT_emit_llvm: {
                Opts.mOutputType = slang::Slang::OT_LLVMAssembly;
                break;
            }
            case OPT_emit_bc: {
                Opts.mOutputType = slang::Slang::OT_Bitcode;
                break;
            }
            case OPT_emit_nothing: {
                Opts.mOutputType = slang::Slang::OT_Nothing;
                break;
            }
            default: {
                slangAssert(false && "Invalid option in output type group!");
            }
            }
        }

        if (Opts.mOutputDep &&
                ((Opts.mOutputType != slang::Slang::OT_Bitcode) &&
                 (Opts.mOutputType != slang::Slang::OT_Dependency)))
            Diags.Report(clang::diag::err_drv_argument_not_allowed_with)
                    << Args->getLastArg(OPT_M_Group)->getAsString(*Args)
                    << Args->getLastArg(OPT_Output_Type_Group)->getAsString(*Args);

        Opts.mAllowRSPrefix = Args->hasArg(OPT_allow_rs_prefix);

        Opts.mJavaReflectionPathBase =
            Args->getLastArgValue(OPT_java_reflection_path_base);
        Opts.mJavaReflectionPackageName =
            Args->getLastArgValue(OPT_java_reflection_package_name);

        llvm::StringRef BitcodeStorageValue =
            Args->getLastArgValue(OPT_bitcode_storage);
        if (BitcodeStorageValue == "ar")
            Opts.mBitcodeStorage = slang::BCST_APK_RESOURCE;
        else if (BitcodeStorageValue == "jc")
            Opts.mBitcodeStorage = slang::BCST_JAVA_CODE;
        else if (!BitcodeStorageValue.empty())
            Diags.Report(clang::diag::err_drv_invalid_value)
                    << OptParser->getOptionName(OPT_bitcode_storage)
                    << BitcodeStorageValue;

        Opts.mOutputDepDir =
            Args->getLastArgValue(OPT_output_dep_dir, Opts.mOutputDir);
        Opts.mAdditionalDepTargets =
            Args->getAllArgValues(OPT_additional_dep_target);

        Opts.mShowHelp = Args->hasArg(OPT_help);
        Opts.mShowVersion = Args->hasArg(OPT_version);

        Opts.mTargetAPI = Args->getLastArgIntValue(OPT_target_api,
                          RS_VERSION,
                          Diags);
    }

    return;
}
예제 #10
0
void ClangDiagnosticConsumer::HandleDiagnostic(
    clang::DiagnosticsEngine::Level clangDiagLevel,
    const clang::Diagnostic &clangDiag) {
  // Handle the module-not-found diagnostic specially if it's a top-level module
  // we're looking for.
  if (clangDiag.getID() == clang::diag::err_module_not_found &&
      CurrentImport && clangDiag.getArgStdStr(0) == CurrentImport->getName()) {
    return;
  }

  const ASTContext &ctx = ImporterImpl.SwiftContext;

  if (clangDiag.getID() == clang::diag::err_module_not_built &&
      CurrentImport && clangDiag.getArgStdStr(0) == CurrentImport->getName()) {
    SourceLoc loc = DiagLoc;
    if (clangDiag.getLocation().isValid())
      loc = resolveSourceLocation(clangDiag.getSourceManager(),
                                  clangDiag.getLocation());

    ctx.Diags.diagnose(loc, diag::clang_cannot_build_module,
                       CurrentImport->getName());
    return;
  }

  // Satisfy the default implementation (bookkeeping).
  if (DumpToStderr)
    TextDiagnosticPrinter::HandleDiagnostic(clangDiagLevel, clangDiag);
  else
    DiagnosticConsumer::HandleDiagnostic(clangDiagLevel, clangDiag);

  // FIXME: Map over source ranges in the diagnostic.
  auto emitDiag = [&ctx, this](clang::FullSourceLoc clangNoteLoc,
                      clang::DiagnosticsEngine::Level clangDiagLevel,
                      StringRef message) {
    decltype(diag::error_from_clang) diagKind;
    switch (clangDiagLevel) {
    case clang::DiagnosticsEngine::Ignored:
      return;
    case clang::DiagnosticsEngine::Note:
      diagKind = diag::note_from_clang;
      break;
    case clang::DiagnosticsEngine::Remark:
      // FIXME: We don't handle remarks yet.
      return;
    case clang::DiagnosticsEngine::Warning:
      diagKind = diag::warning_from_clang;
      break;
    case clang::DiagnosticsEngine::Error:
    case clang::DiagnosticsEngine::Fatal:
      // FIXME: What happens after a fatal error in the importer?
      diagKind = diag::error_from_clang;
      break;
    }

    SourceLoc noteLoc;
    if (clangNoteLoc.isValid())
      noteLoc = resolveSourceLocation(clangNoteLoc.getManager(),
                                      clangNoteLoc);
    ctx.Diags.diagnose(noteLoc, diagKind, message);
  };

  llvm::SmallString<128> message;
  clangDiag.FormatDiagnostic(message);

  if (clangDiag.getLocation().isInvalid()) {
    // Diagnostic about the compiler arguments.
    emitDiag(clang::FullSourceLoc(), clangDiagLevel, message);

  } else {
    assert(clangDiag.hasSourceManager());
    ClangDiagRenderer renderer(ImporterImpl.getClangASTContext(), emitDiag);
    renderer.emitDiagnostic(clangDiag.getLocation(), clangDiagLevel, message,
                            clangDiag.getRanges(), clangDiag.getFixItHints(),
                            &clangDiag.getSourceManager());
  }
}