Skip to content

Commit

Permalink
front end receives corpus
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Apr 2, 2023
1 parent e7dd3c9 commit 0fc0c82
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 114 deletions.
7 changes: 4 additions & 3 deletions include/mrdox/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,17 @@ setupContext(

llvm::Error
doMapping(
Config& cfg);
Corpus& corpus,
Config const& cfg);

/** Build the internal index of the program under analysis.
This must happen before generating docs.
*/
llvm::Error
buildIndex(
Config const& cfg,
Corpus& corpus);
Corpus& corpus,
Config const& cfg);

} // mrdox
} // clang
Expand Down
45 changes: 24 additions & 21 deletions source/lib/ClangDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,16 @@ namespace mrdox {

namespace {

// VFALCO It looks like each created action needs
// its own copy of the Config?
// Maybe because of concurrency.

//------------------------------------------------

struct action
: public clang::ASTFrontendAction
{
explicit
action(
Config& cfg)
: cfg(cfg)
Corpus& corpus,
Config const& cfg) noexcept
: corpus_(corpus)
, cfg_(cfg)
{
}

Expand All @@ -46,32 +43,36 @@ struct action
clang::CompilerInstance& Compiler,
llvm::StringRef InFile) override
{
return std::make_unique<MapASTVisitor>(cfg);
return std::make_unique<MapASTVisitor>(corpus_, cfg_);
}

private:
Config& cfg;
Corpus& corpus_;
Config const& cfg_;
};

//------------------------------------------------

struct factory
: public tooling::FrontendActionFactory
{
explicit
factory(
Config& cfg)
: cfg(cfg)
Corpus& corpus,
Config const& cfg) noexcept
: corpus_(corpus)
, cfg_(cfg)
{
}

std::unique_ptr<FrontendAction>
create() override
{
return std::make_unique<action>(cfg);
return std::make_unique<action>(corpus_, cfg_);
}

Config& cfg;

private:
Corpus& corpus_;
Config const& cfg_;
};

} // (anon)
Expand All @@ -81,18 +82,20 @@ struct factory
std::unique_ptr<
clang::FrontendAction>
makeFrontendAction(
Config& cfg)
Corpus& corpus,
Config const& cfg)
{
return std::make_unique<action>(cfg);
return std::make_unique<action>(corpus, cfg);
}

std::unique_ptr<
tooling::FrontendActionFactory>
newMapperActionFactory(
Config& cfg)
Corpus& corpus,
Config const& cfg)
{
return std::make_unique<factory>(cfg);
return std::make_unique<factory>(corpus, cfg);
}

} // namespace mrdox
} // namespace clang
} // mrdox
} // clang
6 changes: 4 additions & 2 deletions source/lib/ClangDoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ namespace mrdox {
std::unique_ptr<
clang::FrontendAction>
makeFrontendAction(
Config& cfg);
Corpus& corpus,
Config const& cfg);

std::unique_ptr<
tooling::FrontendActionFactory>
newMapperActionFactory(
Config& cfg);
Corpus& corpus,
Config const& cfg);

} // namespace mrdox
} // namespace clang
Expand Down
9 changes: 5 additions & 4 deletions source/lib/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,15 @@ setupContext(

llvm::Error
doMapping(
Config& cfg)
Corpus& corpus,
Config const& cfg)
{
//
// Mapping phase
//
llvm::outs() << "Mapping declarations\n";
auto Err = cfg.Executor->execute(
newMapperActionFactory(cfg),
newMapperActionFactory(corpus, cfg),
cfg.ArgAdjuster);
if(Err)
{
Expand All @@ -225,8 +226,8 @@ doMapping(

llvm::Error
buildIndex(
Config const& cfg,
Corpus& corpus)
Corpus& corpus,
Config const& cfg)
{
// Collect values into output by key.
// In ToolResults, the Key is the hashed USR and the value is the
Expand Down
23 changes: 23 additions & 0 deletions source/lib/Error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// This is a derivative work. originally part of the LLVM Project.
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Copyright (c) 2023 Vinnie Falco ([email protected])
//
// Official repository: https://github.com/cppalliance/mrdox
//

#ifndef MRDOX_SOURCE_ERROR_HPP
#define MRDOX_SOURCE_ERROR_HPP

#include <llvm/Support/Error.h>

namespace clang {
namespace mrdox {

} // mrdox
} // clang

#endif
11 changes: 7 additions & 4 deletions source/lib/Mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "Mapper.h"
#include "BitcodeWriter.h"
#include "Error.hpp"
#include "Serialize.h"
#ifdef _MSC_VER
#pragma warning(push)
Expand Down Expand Up @@ -52,18 +53,20 @@ mapDecl(T const* D)
return true;
bool IsFileInRootDir;
llvm::SmallString<128> File =
getFile(D, D->getASTContext(), cfg.SourceRoot, IsFileInRootDir);
getFile(D, D->getASTContext(), cfg_.SourceRoot, IsFileInRootDir);
auto I = serialize::emitInfo(D, getComment(D, D->getASTContext()),
getLine(D, D->getASTContext()), File,
IsFileInRootDir, cfg.PublicOnly);
IsFileInRootDir, cfg_.PublicOnly);

// A null in place of I indicates that the serializer is skipping this decl
// for some reason (e.g. we're only reporting public decls).
if (I.first)
cfg.ECtx->reportResult(llvm::toHex(llvm::toStringRef(I.first->USR)),
cfg_.ECtx->reportResult(
llvm::toHex(llvm::toStringRef(I.first->USR)),
serialize::serialize(I.first));
if (I.second)
cfg.ECtx->reportResult(llvm::toHex(llvm::toStringRef(I.second->USR)),
cfg_.ECtx->reportResult(
llvm::toHex(llvm::toStringRef(I.second->USR)),
serialize::serialize(I.second));
return true;
}
Expand Down
7 changes: 5 additions & 2 deletions source/lib/Mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ class MapASTVisitor
public:
explicit
MapASTVisitor(
Corpus& corpus,
Config const& cfg)
: cfg(cfg)
: corpus_(corpus)
, cfg_(cfg)
{
}

Expand Down Expand Up @@ -73,7 +75,8 @@ class MapASTVisitor
NamedDecl const* D,
ASTContext const& Context) const;

Config const& cfg;
Corpus& corpus_;
Config const& cfg_;
};

} // mrdox
Expand Down
43 changes: 32 additions & 11 deletions source/lib/Representation.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,40 @@ struct CommentInfo
Children; // List of child comments for this CommentInfo.
};

struct Reference {
struct Reference
{
// This variant (that takes no qualified name parameter) uses the Name as the
// QualName (very useful in unit tests to reduce verbosity). This can't use an
// empty string to indicate the default because we need to accept the empty
// string as a valid input for the global namespace (it will have
// "GlobalNamespace" as the name, but an empty QualName).
Reference(SymbolID USR = SymbolID(), StringRef Name = StringRef(),
Reference(
SymbolID USR = SymbolID(),
StringRef Name = StringRef(),
InfoType IT = InfoType::IT_default)
: USR(USR), Name(Name), QualName(Name), RefType(IT) {}
Reference(SymbolID USR, StringRef Name, InfoType IT, StringRef QualName,
: USR(USR)
, Name(Name)
, QualName(Name)
, RefType(IT)
{
}

Reference(
SymbolID USR,
StringRef Name,
InfoType IT,
StringRef QualName,
StringRef Path = StringRef())
: USR(USR), Name(Name), QualName(QualName), RefType(IT), Path(Path) {}
: USR(USR)
, Name(Name)
, QualName(QualName)
, RefType(IT)
, Path(Path)
{
}

bool operator==(const Reference& Other) const {
bool operator==(const Reference& Other) const
{
return std::tie(USR, Name, QualName, RefType) ==
std::tie(Other.USR, Other.Name, QualName, Other.RefType);
}
Expand All @@ -151,10 +171,10 @@ struct Reference {
SmallString<16> QualName;

InfoType RefType = InfoType::IT_default; // Indicates the type of this
// Reference (namespace, record,
// function, enum, default).
// Path of directory where the clang-doc generated file will be saved
// (possibly unresolved)
// Reference (namespace, record, function, enum, default).

// Path of directory where the mrdox generated file will be saved
// (possibly unresolved)
llvm::SmallString<128> Path;
};

Expand Down Expand Up @@ -215,7 +235,8 @@ struct AccessScope
};

// A base struct for TypeInfos
struct TypeInfo {
struct TypeInfo
{
TypeInfo() = default;
TypeInfo(const Reference& R) : Type(R) {}

Expand Down
14 changes: 9 additions & 5 deletions source/lib/XML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,13 +564,17 @@ renderCodeAsXML(
Config const& cfg)
{
std::unique_ptr<ASTUnit> astUnit =
clang::tooling::buildASTFromCodeWithArgs(cppCode, {});
MapASTVisitor visitor(cfg);
visitor.HandleTranslationUnit(astUnit->getASTContext());
clang::tooling::buildASTFromCodeWithArgs(cppCode, {});
Corpus corpus;
if(llvm::Error err = buildIndex(cfg, corpus))
MapASTVisitor visitor(corpus, cfg);
visitor.HandleTranslationUnit(astUnit->getASTContext());
if(llvm::Error err = buildIndex(corpus, cfg))
return ! err;
return XMLGenerator(cfg).render(xml, corpus, cfg);
bool success = XMLGenerator(cfg).render(xml, corpus, cfg);
// VFALCO oops, cfg.Executor->getToolResults()
// holds information from the previous corpus...
//cfg.Executor->getToolResults()->
return success;
}

//------------------------------------------------
Expand Down
7 changes: 4 additions & 3 deletions source/mrdox/ToolMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ main(int argc, const char** argv)
return EXIT_FAILURE;
}

clang::mrdox::Corpus corpus;

// Extract the AST first
if(llvm::Error err = doMapping(cfg))
if(llvm::Error err = doMapping(corpus, cfg))
{
llvm::errs() <<
toString(std::move(err)) << "\n";
Expand All @@ -95,8 +97,7 @@ main(int argc, const char** argv)

// Build the internal representation of
// the C++ declarations to be documented.
clang::mrdox::Corpus corpus;
if(llvm::Error err = buildIndex(cfg, corpus))
if(llvm::Error err = buildIndex(corpus, cfg))
{
llvm::errs() <<
toString(std::move(err)) << "\n";
Expand Down
Loading

0 comments on commit 0fc0c82

Please sign in to comment.