[cfe-dev] SIGBUS error from StringRef

詹石岩 via cfe-dev cfe-dev at lists.llvm.org
Wed May 4 06:18:08 PDT 2016


I wrote a clang tool based on libtooling. 
This tool will insert some code into the source file through Rewriter. 

I encounted SIGBUS when insert a sentence after a SourceLocation. 
I debugged using CLion and found this SIGBUS happend here: 

Rewriter.cpp, Line 257 @Rewriter::InsertText(SourceLocation Loc, String Ref Str, bool InsertAfter, bool IndentNewLine) 

the code writes: 
while(isWhitespaceExceptNL(MB[i]) //the MB[i] will cause SIGBUS sometimes. 

The MB is a instance of StringRef 

I encounted this problem only for the main.c and utils.c from graph500-2.1.4, in the mpi folder. 

when processing main.c, the problem happens when trying to insert a '\n' after the for statement in line 45: 
for(i=0;i<n;++i)  tmp+=x[i]; 
-------------------^-----//try to insert here 

can anyone tells me how to check the problem? 

I used gcc4.8.4, Linux Mint 17.3, kernel version:3.13.0-24, core i7 4710M

here is my code:
(if I delete the code before if(!isa<CompoundStmt>(stmt)) then the problem disappears)

bool StmtHandler::InsertText(Stmt *stmt, string toInsert) {
    //get rewriter from buffer
    SourceLocation slc = stmt->getLocStart();
    unsigned int fsl = TheContext.getFullLoc(slc).getSpellingColumnNumber();

    string filename = TheContext.getSourceManager().getFilename(slc).str();
    FileIdMap::iterator index = fileMap.find(filename);
    FileID id = TheRewriter.getSourceMgr().getFileID(slc);
    if(index == fileMap.end()){
        fileMap[filename] = id;
        //we need to judge whether the file was instrumented already
        const RewriteBuffer &buffer = TheRewriter.getEditBuffer(id);
        string content(buffer.begin(), buffer.end());
        size_t pos = content.find('\n', 0);
        string start = content.substr(0, pos + 1);
        string comp = "//instrumented "+namespace_origin+"\n";
        //if we can not write the file, we regard it as instrumented
        std::error_code OutErrorInfo;
        std::error_code ok;
        llvm::raw_fd_ostream tmpFile(filename, OutErrorInfo, llvm::sys::fs::F_RW);
        if(OutErrorInfo != ok){
            fileInstrumented[id] = true;
            return false;
        }
        //see if this file was ever instrumented
        if(start != comp){
            fileInstrumented[id] = false;
            TheRewriter.InsertText(TheRewriter.getSourceMgr().getLocForStartOfFile(id), comp,false);
        }else{
            fileInstrumented[id] = true;
            llvm::errs()<<"file "<<filename<<" was ignored.\n";
            return false;
        }

    }else{
        if(fileInstrumented[id])
            return false;
    }

    if(!isa<CompoundStmt>(stmt)){
        SourceLocation ST = stmt->getLocStart();

        TheRewriter.InsertText(ST, "{\n", true, true);
        TheRewriter.InsertText(ST, toInsert + "\n", true, true);
        SourceLocation END = stmt->getLocEnd();
       int offset = Lexer::MeasureTokenLength(END,
                                               TheRewriter.getSourceMgr(),
                                               TheRewriter.getLangOpts()) + 1;

        SourceLocation END1 = END.getLocWithOffset(offset);
        TheRewriter.InsertText(END1, "\n}", true, true);
    }
    else{
        CompoundStmt *compstmt = cast<CompoundStmt>(stmt);
        SourceLocation compLoc = compstmt->getLocStart();
        TheRewriter.InsertText(compLoc.getLocWithOffset(1),"\n"+toInsert+"\n", true, true);
    }
    return true;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160504/c7d2b942/attachment.html>


More information about the cfe-dev mailing list