* compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
*/
#define CINDEX_VERSION_MAJOR 0
-#define CINDEX_VERSION_MINOR 58
+#define CINDEX_VERSION_MINOR 59
#define CINDEX_VERSION_ENCODE(major, minor) ( \
((major) * 10000) \
/**
* 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
};
/**
/// \brief Enumerates the available scopes for skipping function bodies.
enum class SkipFunctionBodiesScope { None, Preamble, PreambleAndMainFile };
+/// \brief Enumerates the available kinds for capturing diagnostics.
+enum class CaptureDiagsKind { None, All, AllWithoutNonErrorsFromIncludes };
+
/// Utility class for loading a ASTContext from an AST file.
class ASTUnit {
public:
bool OnlyLocalDecls = false;
/// Whether to capture any diagnostics produced.
- bool CaptureDiagnostics = false;
+ CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None;
/// Track whether the main file was loaded from an AST or not.
bool MainFileIsAST;
bool UserFilesAreVolatile : 1;
static void ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
- ASTUnit &AST, bool CaptureDiagnostics);
+ ASTUnit &AST, CaptureDiagsKind CaptureDiagnostics);
void TranslateStoredDiagnostics(FileManager &FileMgr,
SourceManager &SrcMan,
/// Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
static std::unique_ptr<ASTUnit>
create(std::shared_ptr<CompilerInvocation> CI,
- IntrusiveRefCntPtr<DiagnosticsEngine> Diags, bool CaptureDiagnostics,
- bool UserFilesAreVolatile);
+ IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
+ CaptureDiagsKind CaptureDiagnostics, bool UserFilesAreVolatile);
enum WhatToLoad {
/// Load options and the preprocessor state.
WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
const FileSystemOptions &FileSystemOpts, bool UseDebugInfo = false,
bool OnlyLocalDecls = false, ArrayRef<RemappedFile> RemappedFiles = None,
- bool CaptureDiagnostics = false, bool AllowPCHWithCompilerErrors = false,
+ CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
+ bool AllowPCHWithCompilerErrors = false,
bool UserFilesAreVolatile = false);
private:
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
FrontendAction *Action = nullptr, ASTUnit *Unit = nullptr,
bool Persistent = true, StringRef ResourceFilesPath = StringRef(),
- bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
+ bool OnlyLocalDecls = false,
+ CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
unsigned PrecompilePreambleAfterNParses = 0,
bool CacheCodeCompletionResults = false,
bool IncludeBriefCommentsInCodeCompletion = false,
std::shared_ptr<CompilerInvocation> CI,
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
- bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
+ bool OnlyLocalDecls = false,
+ CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
unsigned PrecompilePreambleAfterNParses = 0,
TranslationUnitKind TUKind = TU_Complete,
bool CacheCodeCompletionResults = false,
const char **ArgBegin, const char **ArgEnd,
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
- bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
+ bool OnlyLocalDecls = false,
+ CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
ArrayRef<RemappedFile> RemappedFiles = None,
bool RemappedFilesKeepOriginalName = true,
unsigned PrecompilePreambleAfterNParses = 0,
};
/// Diagnostic consumer that saves each diagnostic it is given.
-class StoredDiagnosticConsumer : public DiagnosticConsumer {
+class FilterAndStoreDiagnosticConsumer : public DiagnosticConsumer {
SmallVectorImpl<StoredDiagnostic> *StoredDiags;
SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags;
+ bool CaptureNonErrorsFromIncludes = true;
const LangOptions *LangOpts = nullptr;
SourceManager *SourceMgr = nullptr;
public:
- StoredDiagnosticConsumer(
+ FilterAndStoreDiagnosticConsumer(
SmallVectorImpl<StoredDiagnostic> *StoredDiags,
- SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags)
- : StoredDiags(StoredDiags), StandaloneDiags(StandaloneDiags) {
+ SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags,
+ bool CaptureNonErrorsFromIncludes)
+ : StoredDiags(StoredDiags), StandaloneDiags(StandaloneDiags),
+ CaptureNonErrorsFromIncludes(CaptureNonErrorsFromIncludes) {
assert((StoredDiags || StandaloneDiags) &&
"No output collections were passed to StoredDiagnosticConsumer.");
}
const Diagnostic &Info) override;
};
-/// RAII object that optionally captures diagnostics, if
+/// RAII object that optionally captures and filters diagnostics, if
/// there is no diagnostic client to capture them already.
class CaptureDroppedDiagnostics {
DiagnosticsEngine &Diags;
- StoredDiagnosticConsumer Client;
+ FilterAndStoreDiagnosticConsumer Client;
DiagnosticConsumer *PreviousClient = nullptr;
std::unique_ptr<DiagnosticConsumer> OwningPreviousClient;
public:
CaptureDroppedDiagnostics(
- bool RequestCapture, DiagnosticsEngine &Diags,
+ CaptureDiagsKind CaptureDiagnostics, DiagnosticsEngine &Diags,
SmallVectorImpl<StoredDiagnostic> *StoredDiags,
SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags)
- : Diags(Diags), Client(StoredDiags, StandaloneDiags) {
- if (RequestCapture || Diags.getClient() == nullptr) {
+ : Diags(Diags),
+ Client(StoredDiags, StandaloneDiags,
+ CaptureDiagnostics !=
+ CaptureDiagsKind::AllWithoutNonErrorsFromIncludes) {
+ if (CaptureDiagnostics != CaptureDiagsKind::None ||
+ Diags.getClient() == nullptr) {
OwningPreviousClient = Diags.takeClient();
PreviousClient = Diags.getClient();
Diags.setClient(&Client, false);
makeStandaloneDiagnostic(const LangOptions &LangOpts,
const StoredDiagnostic &InDiag);
-void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level,
- const Diagnostic &Info) {
+static bool isInMainFile(const clang::Diagnostic &D) {
+ if (!D.hasSourceManager() || !D.getLocation().isValid())
+ return false;
+
+ auto &M = D.getSourceManager();
+ return M.isWrittenInMainFile(M.getExpansionLoc(D.getLocation()));
+}
+
+void FilterAndStoreDiagnosticConsumer::HandleDiagnostic(
+ DiagnosticsEngine::Level Level, const Diagnostic &Info) {
// Default implementation (Warnings/errors count).
DiagnosticConsumer::HandleDiagnostic(Level, Info);
// about. This effectively drops diagnostics from modules we're building.
// FIXME: In the long run, ee don't want to drop source managers from modules.
if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr) {
+ if (!CaptureNonErrorsFromIncludes && Level <= DiagnosticsEngine::Warning &&
+ !isInMainFile(Info)) {
+ return;
+ }
+
StoredDiagnostic *ResultDiag = nullptr;
if (StoredDiags) {
StoredDiags->emplace_back(Level, Info);
/// Configure the diagnostics object for use with ASTUnit.
void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
- ASTUnit &AST, bool CaptureDiagnostics) {
+ ASTUnit &AST,
+ CaptureDiagsKind CaptureDiagnostics) {
assert(Diags.get() && "no DiagnosticsEngine was provided");
- if (CaptureDiagnostics)
- Diags->setClient(new StoredDiagnosticConsumer(&AST.StoredDiagnostics, nullptr));
+ if (CaptureDiagnostics != CaptureDiagsKind::None)
+ Diags->setClient(new FilterAndStoreDiagnosticConsumer(
+ &AST.StoredDiagnostics, nullptr,
+ CaptureDiagnostics != CaptureDiagsKind::AllWithoutNonErrorsFromIncludes));
}
std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
const FileSystemOptions &FileSystemOpts, bool UseDebugInfo,
bool OnlyLocalDecls, ArrayRef<RemappedFile> RemappedFiles,
- bool CaptureDiagnostics, bool AllowPCHWithCompilerErrors,
+ CaptureDiagsKind CaptureDiagnostics, bool AllowPCHWithCompilerErrors,
bool UserFilesAreVolatile) {
std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
ASTUnitPreambleCallbacks Callbacks;
{
llvm::Optional<CaptureDroppedDiagnostics> Capture;
- if (CaptureDiagnostics)
- Capture.emplace(/*RequestCapture=*/true, *Diagnostics, &NewPreambleDiags,
+ if (CaptureDiagnostics != CaptureDiagsKind::None)
+ Capture.emplace(CaptureDiagnostics, *Diagnostics, &NewPreambleDiags,
&NewPreambleDiagsStandalone);
// We did not previously compute a preamble, or it can't be reused anyway.
std::unique_ptr<ASTUnit>
ASTUnit::create(std::shared_ptr<CompilerInvocation> CI,
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
- bool CaptureDiagnostics, bool UserFilesAreVolatile) {
+ CaptureDiagsKind CaptureDiagnostics,
+ bool UserFilesAreVolatile) {
std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
ConfigureDiags(Diags, *AST, CaptureDiagnostics);
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FrontendAction *Action,
ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath,
- bool OnlyLocalDecls, bool CaptureDiagnostics,
+ bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
unsigned PrecompilePreambleAfterNParses, bool CacheCodeCompletionResults,
bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile,
std::unique_ptr<ASTUnit> *ErrAST) {
std::shared_ptr<CompilerInvocation> CI,
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
- bool OnlyLocalDecls, bool CaptureDiagnostics,
+ bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
bool UserFilesAreVolatile) {
const char **ArgBegin, const char **ArgEnd,
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
- bool OnlyLocalDecls, bool CaptureDiagnostics,
+ bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
ArrayRef<RemappedFile> RemappedFiles, bool RemappedFilesKeepOriginalName,
unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
// Set up diagnostics, capturing any diagnostics produced.
Clang->setDiagnostics(&Diag);
- CaptureDroppedDiagnostics Capture(true,
+ CaptureDroppedDiagnostics Capture(CaptureDiagsKind::All,
Clang->getDiagnostics(),
&StoredDiagnostics, nullptr);
ProcessWarningOptions(Diag, Inv.getDiagnosticOpts());
--- /dev/null
+#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'
--- /dev/null
+void f(int unusedInHeader) {}
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;
}
modulePath, *pchRdr, ASTUnit::LoadASTOnly, Diags,
FileSystemOpts, /*UseDebugInfo=*/false,
/*OnlyLocalDecls=*/true, None,
- /*CaptureDiagnostics=*/false,
+ CaptureDiagsKind::None,
/*AllowPCHWithCompilerErrors=*/true,
/*UserFilesAreVolatile=*/false);
if (!AU) {
ASTUnit::LoadEverything, Diags,
FileSystemOpts, /*UseDebugInfo=*/false,
CXXIdx->getOnlyLocalDecls(), None,
- /*CaptureDiagnostics=*/true,
+ CaptureDiagsKind::All,
/*AllowPCHWithCompilerErrors=*/true,
/*UserFilesAreVolatile=*/true);
*out_TU = MakeCXTranslationUnit(CXXIdx, std::move(AU));
if (options & CXTranslationUnit_KeepGoing)
Diags->setFatalsAsError(true);
+ CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::All;
+ if (options & CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles)
+ CaptureDiagnostics = CaptureDiagsKind::AllWithoutNonErrorsFromIncludes;
+
// Recover resources if we crash before exiting this function.
llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
Args->data(), Args->data() + Args->size(),
CXXIdx->getPCHContainerOperations(), Diags,
CXXIdx->getClangResourcesPath(), CXXIdx->getOnlyLocalDecls(),
- /*CaptureDiagnostics=*/true, *RemappedFiles.get(),
+ CaptureDiagnostics, *RemappedFiles.get(),
/*RemappedFilesKeepOriginalName=*/true, PrecompilePreambleAfterNParses,
TUKind, CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion,
/*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies, SingleFileParse,
if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
setThreadBackgroundPriority();
- bool CaptureDiagnostics = !Logger::isLoggingEnabled();
+ CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::All;
+ if (TU_options & CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles)
+ CaptureDiagnostics = CaptureDiagsKind::AllWithoutNonErrorsFromIncludes;
+ if (Logger::isLoggingEnabled())
+ CaptureDiagnostics = CaptureDiagsKind::None;
CaptureDiagnosticConsumer *CaptureDiag = nullptr;
- if (CaptureDiagnostics)
+ if (CaptureDiagnostics != CaptureDiagsKind::None)
CaptureDiag = new CaptureDiagnosticConsumer();
// Configure the diagnostics.
PCHContainerOps = std::make_shared<PCHContainerOperations>();
return ASTUnit::LoadFromCompilerInvocation(
- CInvok, PCHContainerOps, Diags, FileMgr, false, false, 0, TU_Complete,
- false, false, isVolatile);
+ CInvok, PCHContainerOps, Diags, FileMgr, false, CaptureDiagsKind::None,
+ 0, TU_Complete, false, false, isVolatile);
}
};
FileManager *FileMgr = new FileManager(FSOpts, VFS);
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
- CI, PCHContainerOpts, Diags, FileMgr, false, false,
- /*PrecompilePreambleAfterNParses=*/1);
+ CI, PCHContainerOpts, Diags, FileMgr, false, CaptureDiagsKind::None,
+ /*PrecompilePreambleAfterNParses=*/1);
return AST;
}