2
2
#include " pystring.h"
3
3
#include " clang/AST/ASTContext.h"
4
4
#include " clang/AST/Decl.h"
5
+ #include " clang/AST/PrettyPrinter.h"
5
6
#include " clang/AST/DeclCXX.h"
6
7
#include " clang/AST/DeclTemplate.h"
7
8
#include " clang/AST/GlobalDecl.h"
10
11
#include " clang/ASTMatchers/ASTMatchers.h"
11
12
#include " clang/Basic/LLVM.h"
12
13
#include " llvm/Support/Casting.h"
14
+ #include " llvm/Support/raw_ostream.h"
13
15
#include < cassert>
14
16
#include < cstdint>
15
17
#include < memory>
@@ -1291,7 +1293,14 @@ void process_concrete_record(const CXXRecordDecl* crd, std::string filename,
1291
1293
void handle_cxx_record_decl (const CXXRecordDecl* crd) {
1292
1294
ASTContext& ctx = crd->getASTContext ();
1293
1295
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
+ }
1295
1304
1296
1305
const auto mng_ctx = ctx.createMangleContext ();
1297
1306
@@ -1421,7 +1430,13 @@ void handle_binding_function(const FunctionDecl* fd) {
1421
1430
1422
1431
ASTContext& ctx = fd->getASTContext ();
1423
1432
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
+ }
1425
1440
std::string filename = sm.getFilename (loc).str ();
1426
1441
1427
1442
// Get the translation unit node we're going to add this Function to
@@ -1442,22 +1457,14 @@ void handle_binding_function(const FunctionDecl* fd) {
1442
1457
std::string body;
1443
1458
if (has_impl_attr (attrs)) {
1444
1459
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);
1461
1468
1462
1469
} else {
1463
1470
SPDLOG_ERROR (" Function {} marked as impl but could not get body" ,
@@ -1498,8 +1505,8 @@ void handle_binding_function(const FunctionDecl* fd) {
1498
1505
(NodeTranslationUnit*)NODES.at (node_function.context ).get ();
1499
1506
// process the namespaces again to make sure we've got the
1500
1507
// 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);
1503
1510
fnptr->context = node_tu->id ;
1504
1511
node_tu->children .push_back (id);
1505
1512
NODES.emplace_back (std::move (fnptr));
0 commit comments