Skip to content

Commit

Permalink
Fix handling of macro calls (#38)
Browse files Browse the repository at this point in the history
* remove debugging notices

* add general (and simpler) fix to macro problem

* removed redundant code

* used expansion line and cols

* removed more debugging statements
  • Loading branch information
ChrisTimperley authored Apr 2, 2019
1 parent d443bfc commit 3d182d9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 25 deletions.
7 changes: 0 additions & 7 deletions cpp/LoopDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,8 @@ json const LoopDB::Entry::to_json() const
void LoopDB::add(clang::ASTContext *ctx, clang::WhileStmt const *stmt)
{
std::string location = build_loc_str(stmt->getSourceRange(), ctx);
llvm::outs() << "fetching body\n";
if (!stmt->getBody()) {
llvm::outs() << "WHILE LOOP HAS NO BODY\n";
}
auto sr = stmt->getBody()->getSourceRange();
if (!sr.isValid())
llvm::outs() << "invalid loop\n";
std::string body = build_loc_str(stmt->getBody()->getSourceRange(), ctx);
llvm::outs() << "fetched body: " << body << "\n";
contents.emplace_back("while", location, body);
}

Expand Down
23 changes: 6 additions & 17 deletions cpp/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,15 @@ std::string const build_loc_str(clang::SourceRange const &range,
clang::SourceManager const &SM = ctx->getSourceManager();
clang::FullSourceLoc loc_begin = ctx->getFullLoc(range.getBegin());
clang::FullSourceLoc loc_end = ctx->getFullLoc(range.getEnd());

// if the starting location corresponds to a macro, we need to extract the
// instantiation location for that macro (otherwise we'll end up with a
// null pointer error later in the function).
//
// https://stackoverflow.com/questions/24062989/clang-fails-replacing-a-statement-if-it-contains-a-macro
if (loc_begin.isMacroID()) {
std::pair<clang::SourceLocation, clang::SourceLocation> expanded =
SM.getImmediateExpansionRange(loc_begin);
loc_begin = ctx->getFullLoc(expanded.first);
}

clang::FileID file_id = SM.getFileID(loc_begin);
clang::FileID file_id = SM.getDecomposedExpansionLoc(range.getBegin()).first;
std::string filename = SM.getFileEntryForID(file_id)->tryGetRealPathName();

std::string s = fmt::format(fmt("{0}@{1}:{2}::{3}:{4}"),
filename,
loc_begin.getSpellingLineNumber(),
loc_begin.getSpellingColumnNumber() - 1,
loc_end.getSpellingLineNumber(),
loc_end.getSpellingColumnNumber());
loc_begin.getExpansionLineNumber(),
loc_begin.getExpansionColumnNumber() - 1,
loc_end.getExpansionLineNumber(),
loc_end.getExpansionColumnNumber());
return s;
}

Expand Down
3 changes: 2 additions & 1 deletion debug.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash
FILE=/experiment/src/Modules/posixmodule.c
docker build -t squareslab/kaskara . \
&& docker create --name kaskara squareslab/kaskara \
&& docker run --volumes-from kaskara --rm -it squareslab/manybugs:python-69223-69224 \
/opt/kaskara/scripts/kaskara-loop-finder /experiment/src/Modules/binascii.c
/opt/kaskara/scripts/kaskara-loop-finder ${FILE}
docker rm -f kaskara

0 comments on commit 3d182d9

Please sign in to comment.