Skip to content

Commit 57f27fd

Browse files
update tests
1 parent 6320f96 commit 57f27fd

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

astgen/src/process_binding.cpp

+27-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "pystring.h"
33
#include "clang/AST/ASTContext.h"
44
#include "clang/AST/Decl.h"
5+
#include "clang/AST/PrettyPrinter.h"
56
#include "clang/AST/DeclCXX.h"
67
#include "clang/AST/DeclTemplate.h"
78
#include "clang/AST/GlobalDecl.h"
@@ -10,6 +11,7 @@
1011
#include "clang/ASTMatchers/ASTMatchers.h"
1112
#include "clang/Basic/LLVM.h"
1213
#include "llvm/Support/Casting.h"
14+
#include "llvm/Support/raw_ostream.h"
1315
#include <cassert>
1416
#include <cstdint>
1517
#include <memory>
@@ -1291,7 +1293,14 @@ void process_concrete_record(const CXXRecordDecl* crd, std::string filename,
12911293
void handle_cxx_record_decl(const CXXRecordDecl* crd) {
12921294
ASTContext& ctx = crd->getASTContext();
12931295
SourceManager& sm = ctx.getSourceManager();
1294-
const auto& loc = crd->getLocation();
1296+
auto loc = crd->getLocation();
1297+
1298+
// we don't care about locations in macros, we always want their expansions
1299+
// if we're using macros to generate functions
1300+
if (loc.isMacroID()) {
1301+
auto range = sm.getExpansionRange(loc);
1302+
loc = range.getBegin();
1303+
}
12951304

12961305
const auto mng_ctx = ctx.createMangleContext();
12971306

@@ -1421,7 +1430,13 @@ void handle_binding_function(const FunctionDecl* fd) {
14211430

14221431
ASTContext& ctx = fd->getASTContext();
14231432
SourceManager& sm = ctx.getSourceManager();
1424-
const auto& loc = fd->getLocation();
1433+
auto loc = fd->getLocation();
1434+
// we don't care about locations in macros, we always want their expansions
1435+
// if we're using macros to generate functions
1436+
if (loc.isMacroID()) {
1437+
auto range = sm.getExpansionRange(loc);
1438+
loc = range.getBegin();
1439+
}
14251440
std::string filename = sm.getFilename(loc).str();
14261441

14271442
// Get the translation unit node we're going to add this Function to
@@ -1442,22 +1457,14 @@ void handle_binding_function(const FunctionDecl* fd) {
14421457
std::string body;
14431458
if (has_impl_attr(attrs)) {
14441459
if (fd->isThisDeclarationADefinition()) {
1445-
auto range = fd->getSourceRange();
1446-
auto begin = sm.getCharacterData(range.getBegin());
1447-
// end points to the last char so we increment to get an iterator
1448-
// end
1449-
auto end = sm.getCharacterData(range.getEnd()) + 1;
1450-
1451-
if (begin && end) {
1452-
auto function_spelling = std::string(begin, end);
1453-
auto remove_macro = ps::replace(function_spelling, "CPPMM_IMPL", "");
1454-
auto rename_function = ps::replace(remove_macro, function_short_name + "(", function_short_name + "_impl(");
1455-
1456-
body = base64::base64_encode(rename_function);
1457-
} else {
1458-
SPDLOG_ERROR("Function {} body iterator are invalid",
1459-
fd->getQualifiedNameAsString());
1460-
}
1460+
std::string s;
1461+
llvm::raw_string_ostream sos(s);
1462+
fd->print(sos);
1463+
1464+
auto function_spelling = s;
1465+
auto remove_macro = ps::replace(function_spelling, "__attribute__((annotate(\"cppmm|impl\")))", "");
1466+
auto rename_function = ps::replace(remove_macro, function_short_name + "(", function_short_name + "_impl(");
1467+
body = base64::base64_encode(rename_function);
14611468

14621469
} else {
14631470
SPDLOG_ERROR("Function {} marked as impl but could not get body",
@@ -1498,8 +1505,8 @@ void handle_binding_function(const FunctionDecl* fd) {
14981505
(NodeTranslationUnit*)NODES.at(node_function.context).get();
14991506
// process the namespaces again to make sure we've got the
15001507
// namespaces in the TU
1501-
// const std::vector<NodeId> namespaces =
1502-
// get_namespaces(fd->getParent(), node_tu);
1508+
const std::vector<NodeId> namespaces =
1509+
get_namespaces(fd->getParent(), node_tu);
15031510
fnptr->context = node_tu->id;
15041511
node_tu->children.push_back(id);
15051512
NODES.emplace_back(std::move(fnptr));

test/mimpl/ref/ast/mimpl.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
"in_binding": false,
118118
"in_library": false,
119119
"noexcept": false,
120-
"definition": "aW50IE51bWJlcl9kb19nZXRfbnVtYmVyX2ltcGwoY29uc3QgOjptaW1wbDo6TnVtYmVyJiBzZWxmKSAgewogICAgcmV0dXJuIHNlbGYuZ2V0X251bWJlcigpOwp9",
120+
"definition": "aW50IE51bWJlcl9kb19nZXRfbnVtYmVyX2ltcGwoY29uc3QgOjptaW1wbDo6TnVtYmVyICZzZWxmKSAgewogICAgcmV0dXJuIHNlbGYuZ2V0X251bWJlcigpOwp9Cg==",
121121
"attributes": [
122122
"cppmm|impl",
123123
"cppmm|rename|Number_do_get_number"

test/mimpl/ref/mimpl-sys/mimpl-c/src/mimpl.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ MIMPL_CPPMM_API unsigned int mimpl__Number_get_number(
2929
}
3030
}
3131

32-
inline int Number_do_get_number_impl(const ::mimpl::Number& self) {
32+
inline int Number_do_get_number_impl(const ::mimpl::Number &self) {
3333
return self.get_number();
3434
}
3535

36+
3637
MIMPL_CPPMM_API unsigned int Number_do_get_number(
3738
int * return_
3839
, mimpl_Number_t const * self)

0 commit comments

Comments
 (0)