Skip to content

Commit b79a2a3

Browse files
committed
use SemaConsumer instead of ASTConsumer
1 parent 175a4d2 commit b79a2a3

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

source/AST/ASTVisitor.cpp

+28-5
Original file line numberDiff line numberDiff line change
@@ -1625,9 +1625,10 @@ ASTVisitor::
16251625
HandleTranslationUnit(
16261626
ASTContext& Context)
16271627
{
1628-
// cache contextual variables
1629-
astContext_ = &Context;
1630-
sourceManager_ = &astContext_->getSourceManager();
1628+
// the ASTContext and Sema better be the same
1629+
// as those set by Initialize and InitializeSema
1630+
Assert(astContext_ == &Context);
1631+
Assert(sema_);
16311632

16321633
// Install handlers for our custom commands
16331634
initCustomCommentCommands(Context);
@@ -1644,8 +1645,6 @@ HandleTranslationUnit(
16441645
if(! config_.shouldVisitTU(File_))
16451646
return;
16461647

1647-
sema_ = &compiler_.getSema();
1648-
16491648
TranslationUnitDecl* TU =
16501649
Context.getTranslationUnitDecl();
16511650
// the traversal scope should *only* consist of the
@@ -1659,5 +1658,29 @@ HandleTranslationUnit(
16591658
TraverseDecl(C);
16601659
}
16611660

1661+
void
1662+
ASTVisitor::
1663+
Initialize(ASTContext& Context)
1664+
{
1665+
astContext_ = &Context;
1666+
sourceManager_ = &Context.getSourceManager();
1667+
}
1668+
1669+
void
1670+
ASTVisitor::
1671+
InitializeSema(Sema& S)
1672+
{
1673+
// Sema should not have been initialized yet
1674+
Assert(! sema_);
1675+
sema_ = &S;
1676+
}
1677+
1678+
void
1679+
ASTVisitor::
1680+
ForgetSema()
1681+
{
1682+
sema_ = nullptr;
1683+
}
1684+
16621685
} // mrdox
16631686
} // clang

source/AST/ASTVisitor.hpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "ConfigImpl.hpp"
1717
#include <mrdox/MetadataFwd.hpp>
18+
#include <clang/Sema/SemaConsumer.h>
1819
#include <clang/Tooling/Execution.h>
1920
#include <optional>
2021
#include <unordered_map>
@@ -35,7 +36,7 @@ namespace mrdox {
3536
more than one translation unit.
3637
*/
3738
class ASTVisitor
38-
: public ASTConsumer
39+
: public SemaConsumer
3940
{
4041
public:
4142
struct FileFilter
@@ -52,14 +53,15 @@ class ASTVisitor
5253

5354
llvm::SmallString<128> usr_;
5455

55-
ASTContext* astContext_;
56-
clang::SourceManager const* sourceManager_;
5756
std::unordered_map<
5857
clang::SourceLocation::UIntTy,
5958
FileFilter> fileFilter_;
6059

6160
clang::CompilerInstance& compiler_;
62-
Sema* sema_;
61+
62+
ASTContext* astContext_ = nullptr;
63+
SourceManager* sourceManager_ = nullptr;
64+
Sema* sema_ = nullptr;
6365

6466
public:
6567
ASTVisitor(
@@ -274,7 +276,6 @@ class ASTVisitor
274276

275277
void HandleTranslationUnit(ASTContext& Context) override;
276278

277-
278279
/** Skip function bodies
279280
280281
This is called by Sema when parsing a function that has a body and:
@@ -286,6 +287,11 @@ class ASTVisitor
286287
nor one that introduces a new type via returning a local class.
287288
*/
288289
bool shouldSkipFunctionBody(Decl* D) override { return true; }
290+
291+
void Initialize(ASTContext& Context) override;
292+
void InitializeSema(Sema& S) override;
293+
void ForgetSema() override;
294+
289295
};
290296

291297
} // mrdox

0 commit comments

Comments
 (0)