Add tests for cpp_macro_definition parsing
This commit is contained in:
parent
0684be5788
commit
58cc481318
5 changed files with 127 additions and 1 deletions
|
|
@ -16,6 +16,8 @@ namespace cppast
|
||||||
class cpp_macro_definition final : public cpp_entity
|
class cpp_macro_definition final : public cpp_entity
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static cpp_entity_kind kind() noexcept;
|
||||||
|
|
||||||
/// \returns A newly built macro definition.
|
/// \returns A newly built macro definition.
|
||||||
/// \notes It is not meant to be registered in the [cppast::cpp_entity_index](),
|
/// \notes It is not meant to be registered in the [cppast::cpp_entity_index](),
|
||||||
/// as no other [cppast::cpp_entity]() can refer to it.
|
/// as no other [cppast::cpp_entity]() can refer to it.
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,16 @@
|
||||||
|
|
||||||
using namespace cppast;
|
using namespace cppast;
|
||||||
|
|
||||||
cpp_entity_kind cpp_macro_definition::do_get_entity_kind() const noexcept
|
cpp_entity_kind cpp_macro_definition::kind() noexcept
|
||||||
{
|
{
|
||||||
return cpp_entity_kind::macro_definition_t;
|
return cpp_entity_kind::macro_definition_t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cpp_entity_kind cpp_macro_definition::do_get_entity_kind() const noexcept
|
||||||
|
{
|
||||||
|
return kind();
|
||||||
|
}
|
||||||
|
|
||||||
cpp_entity_kind cpp_include_directive::do_get_entity_kind() const noexcept
|
cpp_entity_kind cpp_include_directive::do_get_entity_kind() const noexcept
|
||||||
{
|
{
|
||||||
return cpp_entity_kind::include_directive_t;
|
return cpp_entity_kind::include_directive_t;
|
||||||
|
|
|
||||||
59
test/cpp_preprocessor.cpp
Normal file
59
test/cpp_preprocessor.cpp
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
// Copyright (C) 2017 Jonathan Müller <jonathanmueller.dev@gmail.com>
|
||||||
|
// This file is subject to the license terms in the LICENSE file
|
||||||
|
// found in the top-level directory of this distribution.
|
||||||
|
|
||||||
|
#include <cppast/cpp_preprocessor.hpp>
|
||||||
|
|
||||||
|
#include "test_parser.hpp"
|
||||||
|
|
||||||
|
using namespace cppast;
|
||||||
|
|
||||||
|
TEST_CASE("cpp_macro_definition")
|
||||||
|
{
|
||||||
|
auto code = R"(
|
||||||
|
#include <iostream>
|
||||||
|
#define G
|
||||||
|
#define A
|
||||||
|
#define B hello
|
||||||
|
#define C(x, y) x##_name
|
||||||
|
#define D(...) __VA_ARGS__
|
||||||
|
#define E() bar\
|
||||||
|
baz
|
||||||
|
#define F () bar
|
||||||
|
#undef G
|
||||||
|
)";
|
||||||
|
|
||||||
|
auto check_macro = [](const cpp_macro_definition& macro, const char* replacement,
|
||||||
|
const char* args) {
|
||||||
|
REQUIRE(macro.replacement() == replacement);
|
||||||
|
if (args)
|
||||||
|
{
|
||||||
|
REQUIRE(macro.is_function_like());
|
||||||
|
REQUIRE(macro.parameters().value() == args);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
REQUIRE(!macro.is_function_like());
|
||||||
|
REQUIRE(!macro.parameters().has_value());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
auto file = parse({}, "cpp_macro_definition.cpp", code);
|
||||||
|
auto count = test_visit<cpp_macro_definition>(*file, [&](const cpp_macro_definition& macro) {
|
||||||
|
if (macro.name() == "A")
|
||||||
|
check_macro(macro, "", nullptr);
|
||||||
|
else if (macro.name() == "B")
|
||||||
|
check_macro(macro, "hello", nullptr);
|
||||||
|
else if (macro.name() == "C")
|
||||||
|
check_macro(macro, "x##_name", "x,y");
|
||||||
|
else if (macro.name() == "D")
|
||||||
|
check_macro(macro, "__VA_ARGS__", "...");
|
||||||
|
else if (macro.name() == "E")
|
||||||
|
check_macro(macro, "barbaz", "");
|
||||||
|
else if (macro.name() == "F")
|
||||||
|
check_macro(macro, "() bar", nullptr);
|
||||||
|
else
|
||||||
|
REQUIRE(false);
|
||||||
|
});
|
||||||
|
REQUIRE(count == 6u);
|
||||||
|
}
|
||||||
6
test/test.cpp
Normal file
6
test/test.cpp
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
// Copyright (C) 2017 Jonathan Müller <jonathanmueller.dev@gmail.com>
|
||||||
|
// This file is subject to the license terms in the LICENSE file
|
||||||
|
// found in the top-level directory of this distribution.
|
||||||
|
|
||||||
|
#define CATCH_CONFIG_MAIN
|
||||||
|
#include <catch.hpp>
|
||||||
54
test/test_parser.hpp
Normal file
54
test/test_parser.hpp
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
// Copyright (C) 2017 Jonathan Müller <jonathanmueller.dev@gmail.com>
|
||||||
|
// This file is subject to the license terms in the LICENSE file
|
||||||
|
// found in the top-level directory of this distribution.
|
||||||
|
|
||||||
|
#ifndef CPPAST_TEST_PARSER_HPP_INCLUDED
|
||||||
|
#define CPPAST_TEST_PARSER_HPP_INCLUDED
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#include <catch.hpp>
|
||||||
|
|
||||||
|
#include <cppast/cpp_entity_kind.hpp>
|
||||||
|
#include <cppast/libclang_parser.hpp>
|
||||||
|
#include <cppast/visitor.hpp>
|
||||||
|
|
||||||
|
void write_file(const char* name, const char* code)
|
||||||
|
{
|
||||||
|
std::ofstream file(name);
|
||||||
|
file << code;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<cppast::cpp_file> parse(const cppast::cpp_entity_index& idx, const char* name,
|
||||||
|
const char* code)
|
||||||
|
{
|
||||||
|
using namespace cppast;
|
||||||
|
|
||||||
|
write_file(name, code);
|
||||||
|
|
||||||
|
libclang_compile_config config;
|
||||||
|
config.set_flags(cpp_standard::cpp_latest);
|
||||||
|
|
||||||
|
libclang_parser p;
|
||||||
|
return p.parse(idx, name, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename Func>
|
||||||
|
unsigned test_visit(const cppast::cpp_file& file, Func f)
|
||||||
|
{
|
||||||
|
auto count = 0u;
|
||||||
|
cppast::visit(file, [&](const cppast::cpp_entity& e, cppast::visitor_info) {
|
||||||
|
if (e.kind() == T::kind())
|
||||||
|
{
|
||||||
|
auto& obj = static_cast<const T&>(e);
|
||||||
|
f(obj);
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // CPPAST_TEST_PARSER_HPP_INCLUDED
|
||||||
Loading…
Add table
Add a link
Reference in a new issue