Skip to content

Commit 21abfca

Browse files
committed
Refactor test actions
1 parent 521deec commit 21abfca

File tree

7 files changed

+135
-32
lines changed

7 files changed

+135
-32
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ file(GLOB_RECURSE TOOL_SOURCES CONFIGURE_DEPENDS
7878
)
7979

8080
file(GLOB_RECURSE TEST_SOURCES CONFIGURE_DEPENDS
81+
source/tests/*.hpp
8182
source/tests/*.cpp
8283
)
8384

include/mrdox/Config.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct Config
4545
std::string ProjectName;
4646

4747
// Indicates if only public declarations are documented.
48-
bool PublicOnly;
48+
bool PublicOnly = true;
4949

5050
// Directory for outputting generated files.
5151
std::string OutDirectory;

source/lib/XML.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
#include "CorpusVisitor.hpp"
1414
#include "Representation.h"
1515
#include <mrdox/Config.hpp>
16+
#include <clang/Tooling/Execution.h>
1617
#include <clang/Tooling/Tooling.h>
1718
#include <llvm/ADT/StringRef.h>
1819
#include <llvm/Support/FileSystem.h>
1920
#include <llvm/Support/Path.h>
21+
#include <llvm/Support/YAMLParser.h>
2022

2123
//------------------------------------------------
2224
/*
@@ -553,6 +555,33 @@ char const*
553555
XMLGenerator::
554556
Format = "xml";
555557

558+
/** A visitor which keeps its own map of tool results.
559+
*/
560+
class TestVisitor
561+
: public BasicVisitor
562+
{
563+
TestVisitor& corpus_;
564+
tooling::InMemoryToolResults results_;
565+
566+
public:
567+
TestVisitor(
568+
TestVisitor& corpus,
569+
Config const& cfg) noexcept
570+
: BasicVisitor(cfg)
571+
, corpus_(corpus)
572+
{
573+
}
574+
575+
private:
576+
void
577+
reportResult(
578+
StringRef Key,
579+
StringRef Value) override
580+
{
581+
results_.addResult(Key, Value);
582+
}
583+
};
584+
556585
} // (anon)
557586

558587
//------------------------------------------------
File renamed without changes.

source/tests/TestAction.hpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//
2+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
// Copyright (c) 2023 Vinnie Falco ([email protected])
7+
//
8+
// Official repository: https://github.com/cppalliance/mrdox
9+
//
10+
11+
#ifndef MRDOX_TEST_TEST_VISITOR_HPP
12+
#define MRDOX_TEST_TEST_VISITOR_HPP
13+
14+
#include <mrdox/BasicVisitor.hpp>
15+
#include <clang/Tooling/Execution.h>
16+
17+
namespace clang {
18+
namespace mrdox {
19+
20+
//------------------------------------------------
21+
22+
/** A Visitor which stores tool results in a local map
23+
*/
24+
struct TestVisitor : public BasicVisitor
25+
{
26+
explicit
27+
TestVisitor(
28+
Config const& cfg) noexcept
29+
: BasicVisitor(cfg)
30+
{
31+
}
32+
33+
private:
34+
void
35+
reportResult(
36+
llvm::StringRef Key,
37+
llvm::StringRef Value) override
38+
{
39+
results_.addResult(Key, Value);
40+
}
41+
42+
tooling::InMemoryToolResults results_;
43+
};
44+
45+
//------------------------------------------------
46+
47+
struct TestAction
48+
: public clang::ASTFrontendAction
49+
{
50+
explicit
51+
TestAction(
52+
Config const& cfg) noexcept
53+
: cfg_(cfg)
54+
{
55+
}
56+
57+
std::unique_ptr<clang::ASTConsumer>
58+
CreateASTConsumer(
59+
clang::CompilerInstance& Compiler,
60+
llvm::StringRef InFile) override
61+
{
62+
return std::make_unique<TestVisitor>(cfg_);
63+
}
64+
65+
private:
66+
Config const& cfg_;
67+
};
68+
69+
//------------------------------------------------
70+
71+
struct TestFactory
72+
: public tooling::FrontendActionFactory
73+
{
74+
explicit
75+
TestFactory(
76+
Config const& cfg) noexcept
77+
: cfg_(cfg)
78+
{
79+
}
80+
81+
std::unique_ptr<FrontendAction>
82+
create() override
83+
{
84+
return std::make_unique<TestAction>(cfg_);
85+
}
86+
87+
private:
88+
Config const& cfg_;
89+
};
90+
91+
} // mrdox
92+
} // clang
93+
94+
#endif

source/tests/TestMain.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "ClangDoc.h"
1212
#include "Representation.h"
13+
#include "TestAction.hpp"
1314
#include <mrdox/Config.hpp>
1415
#include <mrdox/XML.hpp>
1516
#include <clang/tooling/CompilationDatabase.h>
@@ -78,6 +79,8 @@ struct Reporter
7879
}
7980
};
8081

82+
//------------------------------------------------
83+
8184
/** Return command line arguments as a vector of strings.
8285
*/
8386
std::vector<std::string>
@@ -112,7 +115,8 @@ createExecutor(
112115
#endif
113116
auto executor = std::make_unique<
114117
tooling::StandaloneToolExecutor>(
115-
compilations, llvm::ArrayRef<std::string>{} );
118+
compilations,
119+
compilations.getAllFiles());
116120
if (!executor)
117121
return llvm::make_error<llvm::StringError>(
118122
"could not create StandaloneToolExecutor",
@@ -258,12 +262,11 @@ testMain(int argc, const char** argv)
258262
Args.push_back(argv[i]);
259263

260264
Config cfg;
261-
if(llvm::Error err = setupConfig(cfg, argc, argv))
262-
{
263-
llvm::errs() << "test failure: " << err << "\n";
264-
return EXIT_FAILURE;
265-
}
266265

266+
llvm::Error err = executor->execute(std::make_unique<TestFactory>(cfg));
267+
R.success("execute", err);
268+
269+
#if 0
267270
std::string xml;
268271
for(int i = 1; i < argc; ++i)
269272
{
@@ -359,6 +362,7 @@ testMain(int argc, const char** argv)
359362
iter.increment(ec);
360363
}
361364
}
365+
#endif
362366

363367
if(R.failed)
364368
return EXIT_FAILURE;

source/tests/TestVisitor.hpp

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)