Skip to content

Commit

Permalink
(Issue #66) Map numerical constant and empty macros into the _ module…
Browse files Browse the repository at this point in the history
…, if their headers aren't part of a Clang module.
  • Loading branch information
Syniurge committed Jan 20, 2018
1 parent 514e59f commit d6341da
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
7 changes: 2 additions & 5 deletions ddmd/cpp/calypso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ Loc fromLoc(clang::SourceLocation L)

auto S = SrcMgr.getFilename(L);
loc.filename = S.data();
assert(*(S.data() + S.size()) == '\0'); // TEMPORARY assert to confirm that StringRef isn't needed anymore
loc.linnum = ast()->getSourceManager().getSpellingLineNumber(L);
assert(!S.data() || *(S.data() + S.size()) == '\0'); // TEMPORARY assert to confirm that StringRef isn't needed anymore
loc.linnum = SrcMgr.getSpellingLineNumber(L);

return loc;
}
Expand Down Expand Up @@ -957,9 +957,6 @@ void LangPlugin::buildMacroMap()
if (FoundHeader) break;
}

if (!FoundHeader)
continue;

auto& MacroMapEntry = MacroMap[FoundHeader];
if (!MacroMapEntry)
MacroMapEntry = new MacroMapEntryTy;
Expand Down
24 changes: 15 additions & 9 deletions ddmd/cpp/cppmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,17 @@ static inline bool isTopLevelInNamespaceModule (const clang::Decl *D)
return true;
}

void mapMacros(DeclMapper &mapper, clang::Module::Header* Header, Dsymbols *members)
{
auto MacroMapEntry = calypso.MacroMap[Header];
if (!MacroMapEntry)
return;

for (auto& P: *MacroMapEntry)
if (auto s = mapper.VisitMacro(P.first, P.second))
members->push(s);
}

static void mapNamespace(DeclMapper &mapper,
const clang::DeclContext *DC,
Dsymbols *members,
Expand Down Expand Up @@ -1540,6 +1551,9 @@ static void mapNamespace(DeclMapper &mapper,
if (auto s = mapper.VisitDecl(*D))
members->append(s);
}

if (DC->isTranslationUnit())
mapMacros(mapper, nullptr, members);
}

static void mapClangModule(DeclMapper &mapper,
Expand Down Expand Up @@ -1641,15 +1655,7 @@ static void mapClangModule(DeclMapper &mapper,
{
// Map the macros contained in the module headers (currently limited to numerical constants)
for (auto& Header: M->Headers[clang::Module::HK_Normal])
{
auto MacroMapEntry = calypso.MacroMap[&Header];
if (!MacroMapEntry)
continue;

for (auto& P: *MacroMapEntry)
if (auto s = mapper.VisitMacro(P.first, P.second))
members->push(s);
}
mapMacros(mapper, &Header, members);

for (auto D: RegionDecls)
if (isa<clang::TranslationUnitDecl>(D->getDeclContext()))
Expand Down
17 changes: 17 additions & 0 deletions tests/calypso/macro.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %ldc -cpp-args -std=c++11 -cpp-cachedir=%t.cache -of %t %s
// RUN: %t > %t.out
// RUN: FileCheck %s < %t.out

modmap (C++) "macro.h";

import (C++) _;
import std.stdio : writeln;

void main()
{
writeln("my_macro = ", my_macro);
// CHECK: my_macro = 42

writeln("empty_macro = ", empty_macro);
// CHECK: empty_macro = true
}
4 changes: 4 additions & 0 deletions tests/calypso/macro.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

#define my_macro 42
#define empty_macro

0 comments on commit d6341da

Please sign in to comment.