summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNikolai Kosjar <nikolai.kosjar@qt.io>2018-06-13 09:48:32 +0200
committerIvan Donchevskii <ivan.donchevskii@qt.io>2018-06-21 09:30:56 +0000
commitcc84b351940f2aa9177d0cb83e827be06f219d8f (patch)
tree3b8be0e17d167a86853cae7ab990d3c49b2e2032
parent43ac160b1aa4d98ad07f1e8dde5e445593dc8d91 (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. Change-Id: Ia738a9382b43d210046bff68b3be8cb9dd89206f Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
-rw-r--r--include/clang-c/Index.h17
-rw-r--r--include/clang/Basic/Diagnostic.h5
-rw-r--r--lib/Basic/Diagnostic.cpp1
-rw-r--r--lib/Basic/DiagnosticIDs.cpp8
-rw-r--r--test/Index/ignore-warnings-from-headers.cpp7
-rw-r--r--test/Index/ignore-warnings-from-headers.h1
-rw-r--r--tools/c-index-test/c-index-test.c2
-rw-r--r--tools/libclang/CIndex.cpp3
8 files changed, 41 insertions, 3 deletions
diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h
index 41534f68e6..abf413ab27 100644
--- a/include/clang-c/Index.h
+++ b/include/clang-c/Index.h
@@ -37,6 +37,7 @@
#define CINDEX_VERSION_HAS_PRETTYDECL_BACKPORTED
#define CINDEX_VERSION_HAS_LIMITSKIPFUNCTIONBODIESTOPREAMBLE_BACKPORTED
#define CINDEX_VERSION_HAS_COMPLETION_FIXITS_BACKPORTED
+#define CINDEX_VERSION_HAS_SKIPWARNINGSFROMINCLUDEDFILES_BACKPORTED
#define CINDEX_VERSION_ENCODE(major, minor) ( \
((major) * 10000) \
@@ -1239,9 +1240,9 @@ enum CXTranslationUnit_Flags {
* intent of producing a precompiled header.
*/
CXTranslationUnit_Incomplete = 0x02,
-
+
/**
- * \brief Used to indicate that the translation unit should be built with an
+ * \brief Used to indicate that the translation unit should be built with an
* implicit precompiled header for the preamble.
*
* An implicit precompiled header is used as an optimization when a
@@ -1255,7 +1256,7 @@ enum CXTranslationUnit_Flags {
* precompiled header to improve parsing performance.
*/
CXTranslationUnit_PrecompiledPreamble = 0x04,
-
+
/**
* \brief Used to indicate that the translation unit should cache some
* code-completion results with each reparse of the source file.
@@ -1330,6 +1331,16 @@ enum CXTranslationUnit_Flags {
* The function bodies of the main file are not skipped.
*/
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 a7458d4561..f0b7278916 100644
--- a/include/clang/Basic/Diagnostic.h
+++ b/include/clang/Basic/Diagnostic.h
@@ -180,6 +180,7 @@ private:
unsigned char AllExtensionsSilenced; // Used by __extension__
bool SuppressAfterFatalError; // Suppress diagnostics after a fatal error?
bool SuppressAllDiagnostics; // Suppress all diagnostics.
+ bool SuppressNonErrorsFromIncludedFiles; // Suppress non-errors from all included files.
bool ElideType; // Elide common types of templates.
bool PrintTemplateTree; // Print a tree when comparing templates.
bool ShowColors; // Color printing is enabled.
@@ -550,6 +551,10 @@ public:
}
bool getSuppressAllDiagnostics() const { return SuppressAllDiagnostics; }
+ void setSuppressNonErrorsFromIncludedFiles(bool Val = true) {
+ SuppressNonErrorsFromIncludedFiles = Val;
+ }
+
/// \brief Set type eliding, to skip outputting same types occurring in
/// template types.
void setElideType(bool Val = true) { ElideType = Val; }
diff --git a/lib/Basic/Diagnostic.cpp b/lib/Basic/Diagnostic.cpp
index 26baa838f8..dd15c83e5b 100644
--- a/lib/Basic/Diagnostic.cpp
+++ b/lib/Basic/Diagnostic.cpp
@@ -70,6 +70,7 @@ DiagnosticsEngine::DiagnosticsEngine(IntrusiveRefCntPtr<DiagnosticIDs> diags,
AllExtensionsSilenced = 0;
SuppressAfterFatalError = true;
SuppressAllDiagnostics = false;
+ SuppressNonErrorsFromIncludedFiles = false;
ElideType = true;
PrintTemplateTree = false;
ShowColors = false;
diff --git a/lib/Basic/DiagnosticIDs.cpp b/lib/Basic/DiagnosticIDs.cpp
index c4c425d9eb..ab7802651d 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 45348a21c4..71d73f580a 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 c44c72e2ed..0affc17117 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -3393,6 +3393,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> >