Skip to content

Commit

Permalink
Provide a backup plan if we fail to delay the handling of declarations.
Browse files Browse the repository at this point in the history
This patch enhances the work done in #766 where we assumed the clients are going
to be well-behaved such as clang or clang-repl. However, we can have clients
which connect the clang components in a non-standard way and we should provide a
reasonable fallback.

This patch detects if the setup is non-standard and teaches clad to handle
requests as they come, pretty much the same way as before #766 was implemented.
Of course this comes with a cost when we try to differentiate declarations that
are defined later in the translation unit. We expect such setups to be rare and
mostly in the cases of incremental processing and old repls such as Cling.
  • Loading branch information
vgvassilev committed May 5, 2024
1 parent a8cbd5f commit c529893
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion tools/ClangPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ namespace clad {
SetRequestOptions(opts);
DiffCollector collector(DGR, CladEnabledRange, m_DiffRequestGraph, S,
opts);
// We could not delay the processing of derivatives, inform act as if each
// call is final. That would still have vgvassilev/clad#248 unresolved.
if (!m_Multiplexer)
FinalizeTranslationUnit();

Check warning on line 135 in tools/ClangPlugin.cpp

View check run for this annotation

Codecov / codecov/patch

tools/ClangPlugin.cpp#L135

Added line #L135 was not covered by tests
}

FunctionDecl* CladPlugin::ProcessDiffRequest(DiffRequest& request) {
Expand Down Expand Up @@ -394,7 +398,7 @@ namespace clad {
SetTBRAnalysisOptions(m_DO, opts);
}

void CladPlugin::HandleTranslationUnit(ASTContext& C) {
void CladPlugin::FinalizeTranslationUnit() {
Sema& S = m_CI.getSema();
// Restore the TUScope that became a 0 in Sema::ActOnEndOfTranslationUnit.
if (!m_CI.getPreprocessor().isIncrementalProcessingEnabled())
Expand All @@ -418,7 +422,10 @@ namespace clad {
// Force emission of the produced pending template instantiations.
LocalInstantiations.perform();
GlobalInstantiations.perform();
}

void CladPlugin::HandleTranslationUnit(ASTContext& C) {
FinalizeTranslationUnit();
SendToMultiplexer();
m_Multiplexer->HandleTranslationUnit(C);
}
Expand Down
14 changes: 14 additions & 0 deletions tools/ClangPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Timer.h"

namespace clang {
Expand Down Expand Up @@ -167,6 +168,14 @@ class CladTimerGroup {
AppendDelayed({CallKind::HandleCXXStaticMemberVarInstantiation, D});
}
bool HandleTopLevelDecl(clang::DeclGroupRef D) override {
if (D.isSingleDecl())
if (auto* FD = llvm::dyn_cast<clang::FunctionDecl>(D.getSingleDecl()))
if (m_DFC.IsDerivative(FD)) {
assert(!m_Multiplexer &&

Check warning on line 174 in tools/ClangPlugin.h

View check run for this annotation

Codecov / codecov/patch

tools/ClangPlugin.h#L174

Added line #L174 was not covered by tests
"Must happen only if we failed to rearrange the consumers");
return true;

Check warning on line 176 in tools/ClangPlugin.h

View check run for this annotation

Codecov / codecov/patch

tools/ClangPlugin.h#L176

Added line #L176 was not covered by tests
}

HandleTopLevelDeclForClad(D);
AppendDelayed({CallKind::HandleTopLevelDecl, D});
return true; // happyness, continue parsing
Expand Down Expand Up @@ -250,6 +259,7 @@ class CladTimerGroup {
"Must start from index 0!");
m_DelayedCalls.push_back(DCI);
}
void FinalizeTranslationUnit();
void SendToMultiplexer();
bool CheckBuiltins();
void SetRequestOptions(RequestOptions& opts) const;
Expand All @@ -258,6 +268,10 @@ class CladTimerGroup {
DelayedCallInfo DCI{CallKind::HandleTopLevelDecl, D};
assert(!llvm::is_contained(m_DelayedCalls, DCI) && "Already exists!");
AppendDelayed(DCI);
// We could not delay the process due to some strange way of
// initialization, inform the consumers now.
if (!m_Multiplexer)
m_CI.getASTConsumer().HandleTopLevelDecl(DCI.m_DGR);

Check warning on line 274 in tools/ClangPlugin.h

View check run for this annotation

Codecov / codecov/patch

tools/ClangPlugin.h#L274

Added line #L274 was not covered by tests
}
void HandleTopLevelDeclForClad(clang::DeclGroupRef DGR);
};
Expand Down

0 comments on commit c529893

Please sign in to comment.