clang fails replacing a statement if it contains a macro

后端 未结 2 1960
栀梦
栀梦 2021-02-01 05:31

I\'m using clang to try and parse (with the C++ API) some C++ files and make all the case - break pairs use a specific style.

Example:

*         


        
2条回答
  •  梦毁少年i
    2021-02-01 05:59

    In oder to get the file location related to the macro expansion, an API function can be used to retrieve the information:

    SourceLocation startLoc = rewriter.getSourceMgr().getFileLoc(
        declaration_statement->getLocStart());
    SourceLocation endLoc = rewriter.getSourceMgr().getFileLoc(
        declaration_statement->getLocEnd());
    

    This API function does the same as what Marco wrote in his code, but automatically.

    If we look at the implementation of the function getFileLoc():

    This is the description of the function: Given Loc, if it is a macro location return the expansion location or the spelling location, depending on if it comes from a macro argument or not.

     SourceLocation getFileLoc(SourceLocation Loc) const {
         if (Loc.isFileID()) return Loc;
             return getFileLocSlowCase(Loc);
     }
    
    SourceLocation SourceManager::getFileLocSlowCase(SourceLocation Loc) const {
        do {
            if (isMacroArgExpansion(Loc))
                Loc = getImmediateSpellingLoc(Loc);
            else
                Loc = getImmediateExpansionRange(Loc).first;
        } while (!Loc.isFileID());
        return Loc;
    }
    

提交回复
热议问题