Skip to content

Commit

Permalink
Fix problems with some complex macros
Browse files Browse the repository at this point in the history
When two decls get generated by the same macro, TargetCode compared the
spelling location of their generated source fragments and not their
expanded location. This resulted in TargetCode not adding more than once
function generated from a macro.
  • Loading branch information
manorom committed Feb 8, 2019
1 parent 887b013 commit 30fca0a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion clang/tools/sotoc/src/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extern int SotocDebugLevel;
#define DEBUGP(...) \
do { \
if (SotocDebugLevel > 0) { \
llvm::errs() << "Sotoc: " << llvm::formatv(__VA_ARGS__); \
llvm::errs() << "Sotoc: " << __VA_ARGS__; \
llvm::errs() << "\n"; \
} \
} while (false)
Expand Down
6 changes: 5 additions & 1 deletion clang/tools/sotoc/src/DeclResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ void DeclResolver::addDecl(clang::Decl *D) {
return;
}

DEBUGPDECL(D, "Add declaration to resolver: ");

std::unordered_set<clang::Decl *> UnresolvedDecls;
UnresolvedDecls.insert(D);

Expand Down Expand Up @@ -161,9 +163,11 @@ void DeclResolver::orderAndAddFragments(TargetCode &TC) {
if (!AllDecls.at(orderStack.top()).IsFromSystemHeader) {
auto codeDecl = std::make_shared<TargetCodeDecl>(orderStack.top());

DEBUGPDECL(orderStack.top(), "Generating Fragment for Decl: ");
// TODO: this wont hurt but is not always necessary
codeDecl->NeedsSemicolon = true;
TC.addCodeFragmentFront(codeDecl);
bool added = TC.addCodeFragmentFront(codeDecl);
DEBUGP("Decl was a duplicate: " << !added);
}
orderStack.pop();
}
Expand Down
4 changes: 2 additions & 2 deletions clang/tools/sotoc/src/TargetCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void TargetCode::generateCode(llvm::raw_ostream &Out) {

// This is a workaround, since "Decl::print" includes "pragma omp declare".
if (PrettyCode != "")
TargetCodeRewriter.ReplaceText(Frag->getInnerRange(), PrettyCode);
TargetCodeRewriter.ReplaceText(Frag->getSpellingRange(), PrettyCode);

if (TCR) {
generateFunctionPrologue(TCR);
Expand All @@ -85,7 +85,7 @@ void TargetCode::generateCode(llvm::raw_ostream &Out) {
generateFunctionEpilogue(TCR);
}
Out << "\n";
Out << TargetCodeRewriter.getRewrittenText(Frag->getInnerRange());
Out << TargetCodeRewriter.getRewrittenText(Frag->getSpellingRange());

if (Frag->NeedsSemicolon) {
Out << ";";
Expand Down
19 changes: 17 additions & 2 deletions clang/tools/sotoc/src/TargetCodeFragment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ clang::SourceRange TargetCodeRegion::getRealRange() {
return CapturedStmtNode->getSourceRange();
}

clang::SourceRange TargetCodeRegion::getSpellingRange() {
auto &SM = CapturedStmtNode->getCapturedDecl()->getASTContext().getSourceManager();
auto InnerRange = getInnerRange();
return clang::SourceRange(SM.getSpellingLoc(InnerRange.getBegin()),
SM.getSpellingLoc(InnerRange.getEnd()));
}

clang::SourceRange TargetCodeRegion::getInnerRange() {
auto InnerLocStart = getStartLoc();
auto InnerLocEnd = getEndLoc();
Expand Down Expand Up @@ -428,9 +435,17 @@ std::string TargetCodeRegion::PrintPretty() {
clang::SourceRange TargetCodeDecl::getRealRange() {
// return DeclNode->getSourceRange();
// return DeclNode->getSourceRange();
//auto &SM = DeclNode->getASTContext().getSourceManager();
//return clang::SourceRange(SM.getSpellingLoc(DeclNode->getBeginLoc()),
// SM.getSpellingLoc(DeclNode->getEndLoc()));
return DeclNode->getSourceRange();
}

clang::SourceRange TargetCodeDecl::getSpellingRange() {
auto &SM = DeclNode->getASTContext().getSourceManager();
return clang::SourceRange(SM.getSpellingLoc(DeclNode->getBeginLoc()),
SM.getSpellingLoc(DeclNode->getEndLoc()));
auto InnerRange = getInnerRange();
return clang::SourceRange(SM.getSpellingLoc(InnerRange.getBegin()),
SM.getSpellingLoc(InnerRange.getEnd()));
}

std::string TargetCodeDecl::PrintPretty() {
Expand Down
5 changes: 5 additions & 0 deletions clang/tools/sotoc/src/TargetCodeFragment.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ class TargetCodeFragment {
/// Gets the 'inner' source range. This can differ for target regions from the
/// source range.
virtual clang::SourceRange getInnerRange() { return getRealRange(); };
/// Get the spelling source range. That is the range without macro
/// expansions.
virtual clang::SourceRange getSpellingRange() = 0;
/// Accessor to TargetCodeKind
clang::OpenMPDirectiveKind getTargetCodeKind() { return TargetCodeKind; };
};
Expand Down Expand Up @@ -140,6 +143,7 @@ class TargetCodeRegion : public TargetCodeFragment {
virtual std::string PrintPretty() override;
clang::SourceRange getRealRange() override;
clang::SourceRange getInnerRange() override;
clang::SourceRange getSpellingRange() override;
/// Returns a source location at the start of a pragma in the captured
/// statment.
clang::SourceLocation getStartLoc();
Expand Down Expand Up @@ -174,4 +178,5 @@ class TargetCodeDecl : public TargetCodeFragment {

virtual std::string PrintPretty() override;
clang::SourceRange getRealRange() override;
clang::SourceRange getSpellingRange() override;
};
1 change: 1 addition & 0 deletions clang/tools/sotoc/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class SourceTransformAction : public clang::ASTFrontendAction {
// std::error_code error_code;
// llvm::raw_fd_ostream outFile("output.txt", error_code,
// llvm::sys::fs::F_Append);
DEBUGP("Generating Code§");
Code->generateCode(llvm::outs());
// outFile.close();
delete Code;
Expand Down

0 comments on commit 30fca0a

Please sign in to comment.