Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mrdox-test supports yml configs. #112

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/lib/Lib/SingleFileDB.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,23 @@ class SingleFileDB
std::vector<tooling::CompileCommand> cc_;

public:
explicit
SingleFileDB(
llvm::StringRef pathName)
llvm::StringRef pathName,
std::string_view cxxstd = "c++20",
std::span<const std::string> extra_flags = {},
std::string_view heuristic = "unit test")
{
auto fileName = files::getFileName(pathName);
auto parentDir = files::getParentDir(pathName);

std::vector<std::string> cmds;
cmds.emplace_back("clang");
cmds.emplace_back("-fsyntax-only");
cmds.emplace_back("-std=c++20");
llvm::raw_string_ostream{cmds.emplace_back()} << "-std=" << cxxstd;

for (const auto & fl : extra_flags)
cmds.emplace_back(fl);

cmds.emplace_back("-pedantic-errors");
cmds.emplace_back("-Werror");
cmds.emplace_back(fileName);
Expand All @@ -47,7 +53,7 @@ class SingleFileDB
fileName,
std::move(cmds),
parentDir);
cc_.back().Heuristic = "unit test";
cc_.back().Heuristic = heuristic;
}

std::vector<tooling::CompileCommand>
Expand Down
72 changes: 72 additions & 0 deletions src/test/TestConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 Klemens D. Morgenstern
//

#include "TestConfig.hpp"
#include <mrdox/Support/Error.hpp>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/Path.h>
#include <llvm/Support/YAMLParser.h>
#include <llvm/Support/YAMLTraits.h>


template<>
struct llvm::yaml::MappingTraits<
clang::mrdox::TestConfig>
{
static void mapping(IO &io,
clang::mrdox::TestConfig& f)
{
io.mapOptional("cxxstd", f.cxxstd);
io.mapOptional("compile-flags", f.compile_flags);
io.mapOptional("should-fail", f.should_fail);
io.mapOptional("heuristics", f.heuristics);
}
};

namespace clang {
namespace mrdox {

Expected<std::vector<TestConfig>>
TestConfig::loadForTest(
std::string_view dir,
std::string_view file)
{
namespace fs = llvm::sys::fs;
namespace path = llvm::sys::path;

llvm::SmallString<256> filePath{file.begin(), file.end()};
path::replace_extension(filePath, "yml");

if (!fs::exists(filePath))
{
filePath = dir;
filePath.append("/mrdox-test.yml");
}

if (fs::exists(filePath))
{
auto fileText = llvm::MemoryBuffer::getFile(filePath);
if(! fileText)
return Error(fileText.getError());
llvm::yaml::Input yin(**fileText);

std::vector<TestConfig> res;
do
{
yin >> res.emplace_back();
if (auto ec = yin.error())
return Error(ec);
}
while (yin.nextDocument());
return res;
}
return std::vector<TestConfig>{TestConfig{}};
}

}
}
42 changes: 42 additions & 0 deletions src/test/TestConfig.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 Klemens D. Morgenstern
//

#ifndef MRDOX_TEST_CONFIG_HPP
#define MRDOX_TEST_CONFIG_HPP

#include <mrdox/Platform.hpp>
#include <mrdox/Support/Error.hpp>

#include <string>
#include <string_view>
#include <vector>

namespace clang {
namespace mrdox {

struct TestConfig
{
std::string cxxstd = "c++20";
std::vector<std::string> compile_flags;
bool should_fail = false;
std::string heuristics = "unit test";

MRDOX_DECL
static
Expected<std::vector<TestConfig>>
loadForTest(
std::string_view dir,
std::string_view file);
};


}
}


#endif //MRDOX_TEST_CONFIG_HPP
30 changes: 20 additions & 10 deletions src/test/TestRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//

#include "TestRunner.hpp"
#include "TestConfig.hpp"
#include "TestArgs.hpp"
#include "lib/Support/Error.hpp"
#include "lib/Support/Path.hpp"
Expand Down Expand Up @@ -112,16 +113,25 @@ handleFile(
// Build Corpus
std::unique_ptr<Corpus> corpus;
{
SingleFileDB db1(filePath);
auto workingDir = files::getParentDir(filePath);
// Convert relative paths to absolute
AbsoluteCompilationDatabase db(
llvm::StringRef(workingDir), db1, config);
ToolExecutor ex(report::Level::debug, *config, db);
auto result = CorpusImpl::build(ex, config);
if(! result)
report::error("{}: \"{}\"", result.error(), filePath);
corpus = result.release();

auto tcs = TestConfig::loadForTest(dirPath.c_str(), filePath);
if (!tcs)
return report::error("{}: \"{}\"", tcs.error(), dirPath);

for (const auto & tc : *tcs)
{
SingleFileDB db1(filePath, tc.cxxstd, tc.compile_flags, tc.heuristics);
auto workingDir = files::getParentDir(filePath);
// Convert relative paths to absolute
AbsoluteCompilationDatabase db(
llvm::StringRef(workingDir), db1, config);
ToolExecutor ex(report::Level::debug, *config, db);
auto result = CorpusImpl::build(ex, config);
if(! result)
report::error("{}: \"{}\"", result.error(), filePath);
if (!corpus)
corpus = result.release();
}
}

// Generate XML
Expand Down
4 changes: 4 additions & 0 deletions test-files/old-tests/attributes_1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cxxstd: c++20
...
---
cxxstd: c++17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... no, I don't think we should invent a new yml file format that has multiple yml configurations inside.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not a new yaml format, that's standard yaml.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I see that but still.... where are you

2 changes: 2 additions & 0 deletions test-files/old-tests/explicit-conv-operator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cxxstd: c++20
# no C++17 here.
2 changes: 2 additions & 0 deletions test-files/old-tests/explicit-ctor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cxxstd: c++20
# no C++17 here.
2 changes: 2 additions & 0 deletions test-files/old-tests/explicit-deduct-guide.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cxxstd: c++20
# no C++17 here.
4 changes: 4 additions & 0 deletions test-files/old-tests/mrdox-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cxxstd: c++20
...
---
cxxstd: c++2b
Loading