Index: include/clang-c/Index.h =================================================================== --- include/clang-c/Index.h +++ include/clang-c/Index.h @@ -1332,7 +1332,17 @@ * * The function bodies of the main file are not skipped. */ - CXTranslationUnit_LimitSkipFunctionBodiesToPreamble = 0x800 + CXTranslationUnit_LimitSkipFunctionBodiesToPreamble = 0x800, + + /** + * Used to indicate that non-errors from included files should be ignored. + * + * If set, clang_getDiagnosticSetFromTU() will not report e.g. warnings from + * included files anymore. This speeds up clang_getDiagnosticSetFromTU() for + * the case where these warnings are not of interest, as for an IDE for + * example, which typically shows only the diagnostics in the main file. + */ + CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles = 0x1000 }; /** Index: include/clang/Basic/Diagnostic.h =================================================================== --- include/clang/Basic/Diagnostic.h +++ include/clang/Basic/Diagnostic.h @@ -213,6 +213,9 @@ // Suppress all diagnostics. bool SuppressAllDiagnostics = false; + // Suppress non-errors from all included files. + bool SuppressNonErrorsFromIncludedFiles = false; + // Elide common types of templates. bool ElideType = true; @@ -634,6 +637,10 @@ } bool getSuppressAllDiagnostics() const { return SuppressAllDiagnostics; } + void setSuppressNonErrorsFromIncludedFiles(bool Val = true) { + SuppressNonErrorsFromIncludedFiles = Val; + } + /// Set type eliding, to skip outputting same types occurring in /// template types. void setElideType(bool Val = true) { ElideType = Val; } Index: lib/Basic/DiagnosticIDs.cpp =================================================================== --- lib/Basic/DiagnosticIDs.cpp +++ lib/Basic/DiagnosticIDs.cpp @@ -477,6 +477,14 @@ Result = diag::Severity::Fatal; } + // If requested, ignore non-errors from all included files. + if (Diag.SuppressNonErrorsFromIncludedFiles && + Result <= diag::Severity::Warning && Loc.isValid() && + !Diag.getSourceManager().isInMainFile( + Diag.getSourceManager().getExpansionLoc(Loc))) { + return diag::Severity::Ignored; + } + // Custom diagnostics always are emitted in system headers. bool ShowInSystemHeader = !GetDiagInfo(DiagID) || GetDiagInfo(DiagID)->WarnShowInSystemHeader; Index: test/Index/ignore-warnings-from-headers.h =================================================================== --- /dev/null +++ test/Index/ignore-warnings-from-headers.h @@ -0,0 +1 @@ +void f(int unusedInHeader) {} Index: test/Index/ignore-warnings-from-headers.cpp =================================================================== --- /dev/null +++ test/Index/ignore-warnings-from-headers.cpp @@ -0,0 +1,7 @@ +#include "ignore-warnings-from-headers.h" + +void g(int unusedInMainFile) {} + +// RUN: env CINDEXTEST_IGNORE_NONERRORS_FROM_INCLUDED_FILES=1 c-index-test -test-load-source function %s -Wunused-parameter 2>&1 | FileCheck %s +// CHECK-NOT: warning: unused parameter 'unusedInHeader' +// CHECK: warning: unused parameter 'unusedInMainFile' Index: tools/c-index-test/c-index-test.c =================================================================== --- tools/c-index-test/c-index-test.c +++ tools/c-index-test/c-index-test.c @@ -84,6 +84,8 @@ options |= CXTranslationUnit_KeepGoing; if (getenv("CINDEXTEST_LIMIT_SKIP_FUNCTION_BODIES_TO_PREAMBLE")) options |= CXTranslationUnit_LimitSkipFunctionBodiesToPreamble; + if (getenv("CINDEXTEST_IGNORE_NONERRORS_FROM_INCLUDED_FILES")) + options |= CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles; return options; } Index: tools/libclang/CIndex.cpp =================================================================== --- tools/libclang/CIndex.cpp +++ tools/libclang/CIndex.cpp @@ -3397,6 +3397,9 @@ if (options & CXTranslationUnit_KeepGoing) Diags->setSuppressAfterFatalError(false); + if (options & CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles) + Diags->setSuppressNonErrorsFromIncludedFiles(true); + // Recover resources if we crash before exiting this function. llvm::CrashRecoveryContextCleanupRegistrar >