diff options
author | Nikolai Kosjar <nikolai.kosjar@qt.io> | 2018-06-13 09:48:32 +0200 |
---|---|---|
committer | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-10-02 06:30:13 +0000 |
commit | 518cdebf02e01588cb18760aa39bd34f9df75f78 (patch) | |
tree | bd7646ccc52d6f4665dc520824cadd0b32e52b67 | |
parent | 9e8237e257bcff8dbf501994837cf8e7b94591e5 (diff) |
[libclang] Allow skipping warnings from all included files
------------------------------------------------------------------
* https://reviews.llvm.org/D48116
------------------------------------------------------------------
Depending on the included files and the used warning flags, e.g. -
Weverything, a huge number of warnings can be reported for included
files. As processing that many diagnostics comes with a performance
impact and not all clients are interested in those diagnostics, add a
flag to skip them.
This is a cherry pick from commit cc84b351940f2aa9177d0cb83e827be06f219d8f
Change-Id: Ia738a9382b43d210046bff68b3be8cb9dd89206f
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
-rw-r--r-- | include/clang-c/Index.h | 13 | ||||
-rw-r--r-- | include/clang/Basic/Diagnostic.h | 7 | ||||
-rw-r--r-- | include/clang/Frontend/ASTUnit.h | 1 | ||||
-rw-r--r-- | lib/Basic/DiagnosticIDs.cpp | 8 | ||||
-rw-r--r-- | test/Index/ignore-warnings-from-headers.cpp | 7 | ||||
-rw-r--r-- | test/Index/ignore-warnings-from-headers.h | 1 | ||||
-rw-r--r-- | tools/c-index-test/c-index-test.c | 2 | ||||
-rw-r--r-- | tools/libclang/CIndex.cpp | 3 |
8 files changed, 41 insertions, 1 deletions
diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index 65dada38b0..0dce9f2f27 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -33,6 +33,7 @@ */ #define CINDEX_VERSION_MAJOR 0 #define CINDEX_VERSION_MINOR 49 +#define CINDEX_VERSION_HAS_SKIPWARNINGSFROMINCLUDEDFILES_BACKPORTED #define CINDEX_VERSION_ENCODE(major, minor) ( \ ((major) * 10000) \ @@ -1332,7 +1333,17 @@ enum CXTranslationUnit_Flags { * * 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 }; /** diff --git a/include/clang/Basic/Diagnostic.h b/include/clang/Basic/Diagnostic.h index d30ac14e02..89ccb3e0e2 100644 --- a/include/clang/Basic/Diagnostic.h +++ b/include/clang/Basic/Diagnostic.h @@ -213,6 +213,9 @@ private: // 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 @@ public: } 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; } diff --git a/include/clang/Frontend/ASTUnit.h b/include/clang/Frontend/ASTUnit.h index a58cf53943..88c8370c24 100644 --- a/include/clang/Frontend/ASTUnit.h +++ b/include/clang/Frontend/ASTUnit.h @@ -43,6 +43,7 @@ #include <cstddef> #include <cstdint> #include <memory> +#include <mutex> #include <string> #include <utility> #include <vector> diff --git a/lib/Basic/DiagnosticIDs.cpp b/lib/Basic/DiagnosticIDs.cpp index 8f2c3d06a5..c50da06d3b 100644 --- a/lib/Basic/DiagnosticIDs.cpp +++ b/lib/Basic/DiagnosticIDs.cpp @@ -477,6 +477,14 @@ DiagnosticIDs::getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc, Result = diag::Severity::Fatal; } + // If requested, ignore warnings from all headers. + 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; diff --git a/test/Index/ignore-warnings-from-headers.cpp b/test/Index/ignore-warnings-from-headers.cpp new file mode 100644 index 0000000000..3b8d765c4a --- /dev/null +++ b/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' diff --git a/test/Index/ignore-warnings-from-headers.h b/test/Index/ignore-warnings-from-headers.h new file mode 100644 index 0000000000..84c249a7f5 --- /dev/null +++ b/test/Index/ignore-warnings-from-headers.h @@ -0,0 +1 @@ +void f(int unusedInHeader) {} diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 70ab11866e..b7f9cdc518 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -84,6 +84,8 @@ static unsigned getDefaultParsingOptions() { 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; } diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index 58d565fa82..505bd0a02f 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -3399,6 +3399,9 @@ clang_parseTranslationUnit_Impl(CXIndex CIdx, const char *source_filename, 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<DiagnosticsEngine, llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |