Skip to content

Commit

Permalink
Refactor test actions
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Apr 4, 2023
1 parent 521deec commit 21abfca
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 32 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ file(GLOB_RECURSE TOOL_SOURCES CONFIGURE_DEPENDS
)

file(GLOB_RECURSE TEST_SOURCES CONFIGURE_DEPENDS
source/tests/*.hpp
source/tests/*.cpp
)

Expand Down
2 changes: 1 addition & 1 deletion include/mrdox/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct Config
std::string ProjectName;

// Indicates if only public declarations are documented.
bool PublicOnly;
bool PublicOnly = true;

// Directory for outputting generated files.
std::string OutDirectory;
Expand Down
29 changes: 29 additions & 0 deletions source/lib/XML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
#include "CorpusVisitor.hpp"
#include "Representation.h"
#include <mrdox/Config.hpp>
#include <clang/Tooling/Execution.h>
#include <clang/Tooling/Tooling.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/Path.h>
#include <llvm/Support/YAMLParser.h>

//------------------------------------------------
/*
Expand Down Expand Up @@ -553,6 +555,33 @@ char const*
XMLGenerator::
Format = "xml";

/** A visitor which keeps its own map of tool results.
*/
class TestVisitor
: public BasicVisitor
{
TestVisitor& corpus_;
tooling::InMemoryToolResults results_;

public:
TestVisitor(
TestVisitor& corpus,
Config const& cfg) noexcept
: BasicVisitor(cfg)
, corpus_(corpus)
{
}

private:
void
reportResult(
StringRef Key,
StringRef Value) override
{
results_.addResult(Key, Value);
}
};

} // (anon)

//------------------------------------------------
Expand Down
File renamed without changes.
94 changes: 94 additions & 0 deletions source/tests/TestAction.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// 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_TEST_TEST_VISITOR_HPP
#define MRDOX_TEST_TEST_VISITOR_HPP

#include <mrdox/BasicVisitor.hpp>
#include <clang/Tooling/Execution.h>

namespace clang {
namespace mrdox {

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

/** A Visitor which stores tool results in a local map
*/
struct TestVisitor : public BasicVisitor
{
explicit
TestVisitor(
Config const& cfg) noexcept
: BasicVisitor(cfg)
{
}

private:
void
reportResult(
llvm::StringRef Key,
llvm::StringRef Value) override
{
results_.addResult(Key, Value);
}

tooling::InMemoryToolResults results_;
};

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

struct TestAction
: public clang::ASTFrontendAction
{
explicit
TestAction(
Config const& cfg) noexcept
: cfg_(cfg)
{
}

std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(
clang::CompilerInstance& Compiler,
llvm::StringRef InFile) override
{
return std::make_unique<TestVisitor>(cfg_);
}

private:
Config const& cfg_;
};

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

struct TestFactory
: public tooling::FrontendActionFactory
{
explicit
TestFactory(
Config const& cfg) noexcept
: cfg_(cfg)
{
}

std::unique_ptr<FrontendAction>
create() override
{
return std::make_unique<TestAction>(cfg_);
}

private:
Config const& cfg_;
};

} // mrdox
} // clang

#endif
16 changes: 10 additions & 6 deletions source/tests/TestMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "ClangDoc.h"
#include "Representation.h"
#include "TestAction.hpp"
#include <mrdox/Config.hpp>
#include <mrdox/XML.hpp>
#include <clang/tooling/CompilationDatabase.h>
Expand Down Expand Up @@ -78,6 +79,8 @@ struct Reporter
}
};

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

/** Return command line arguments as a vector of strings.
*/
std::vector<std::string>
Expand Down Expand Up @@ -112,7 +115,8 @@ createExecutor(
#endif
auto executor = std::make_unique<
tooling::StandaloneToolExecutor>(
compilations, llvm::ArrayRef<std::string>{} );
compilations,
compilations.getAllFiles());
if (!executor)
return llvm::make_error<llvm::StringError>(
"could not create StandaloneToolExecutor",
Expand Down Expand Up @@ -258,12 +262,11 @@ testMain(int argc, const char** argv)
Args.push_back(argv[i]);

Config cfg;
if(llvm::Error err = setupConfig(cfg, argc, argv))
{
llvm::errs() << "test failure: " << err << "\n";
return EXIT_FAILURE;
}

llvm::Error err = executor->execute(std::make_unique<TestFactory>(cfg));
R.success("execute", err);

#if 0
std::string xml;
for(int i = 1; i < argc; ++i)
{
Expand Down Expand Up @@ -359,6 +362,7 @@ testMain(int argc, const char** argv)
iter.increment(ec);
}
}
#endif

if(R.failed)
return EXIT_FAILURE;
Expand Down
25 changes: 0 additions & 25 deletions source/tests/TestVisitor.hpp

This file was deleted.

0 comments on commit 21abfca

Please sign in to comment.