From 13d49cfcd2f937320b21d819b199089a8cceebc4 Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Wed, 13 Jun 2018 09:48:32 +0200 Subject: [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: If90a682795cad580abf507f5988e1c9b1e08d582 Reviewed-by: Orgad Shaneh --- include/clang-c/Index.h | 13 ++++++++++++- include/clang/Basic/Diagnostic.h | 7 +++++++ include/clang/Frontend/ASTUnit.h | 1 + lib/Basic/DiagnosticIDs.cpp | 8 ++++++++ test/Index/ignore-warnings-from-headers.cpp | 7 +++++++ test/Index/ignore-warnings-from-headers.h | 1 + tools/c-index-test/c-index-test.c | 2 ++ tools/libclang/CIndex.cpp | 3 +++ 8 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/Index/ignore-warnings-from-headers.cpp create mode 100644 test/Index/ignore-warnings-from-headers.h diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index c51dfb1598..434357121a 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 50 +#define CINDEX_VERSION_HAS_SKIPWARNINGSFROMINCLUDEDFILES_BACKPORTED #define CINDEX_VERSION_ENCODE(major, minor) ( \ ((major) * 10000) \ @@ -1341,7 +1342,17 @@ enum CXTranslationUnit_Flags { /** * Used to indicate that implicit attributes should be visited. */ - CXTranslationUnit_VisitImplicitAttributes = 0x2000 + CXTranslationUnit_VisitImplicitAttributes = 0x2000, + + /** + * 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 = 0x4000 }; /** diff --git a/include/clang/Basic/Diagnostic.h b/include/clang/Basic/Diagnostic.h index a516721ace..e0939ecfd5 100644 --- a/include/clang/Basic/Diagnostic.h +++ b/include/clang/Basic/Diagnostic.h @@ -216,6 +216,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; @@ -635,6 +638,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 914ed8b9ee..7e8b28a526 100644 --- a/include/clang/Frontend/ASTUnit.h +++ b/include/clang/Frontend/ASTUnit.h @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include 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 fc6ba46fd6..855746b658 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -88,6 +88,8 @@ static unsigned getDefaultParsingOptions() { options |= CXTranslationUnit_IncludeAttributedTypes; if (getenv("CINDEXTEST_VISIT_IMPLICIT_ATTRIBUTES")) options |= CXTranslationUnit_VisitImplicitAttributes; + 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 c89393a476..310374c46b 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -3411,6 +3411,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 > -- cgit v1.2.3