diff options
Diffstat (limited to 'tools/libclang/CXSourceLocation.cpp')
-rw-r--r-- | tools/libclang/CXSourceLocation.cpp | 99 |
1 files changed, 65 insertions, 34 deletions
diff --git a/tools/libclang/CXSourceLocation.cpp b/tools/libclang/CXSourceLocation.cpp index a6bf8fc..bc8d575 100644 --- a/tools/libclang/CXSourceLocation.cpp +++ b/tools/libclang/CXSourceLocation.cpp @@ -12,15 +12,17 @@ //===----------------------------------------------------------------------===// #include "clang/Frontend/ASTUnit.h" - #include "CIndexer.h" -#include "CXString.h" +#include "CLog.h" +#include "CXLoadedDiagnostic.h" #include "CXSourceLocation.h" +#include "CXString.h" #include "CXTranslationUnit.h" -#include "CXLoadedDiagnostic.h" +#include "llvm/Support/Compiler.h" +#include "llvm/Support/Format.h" using namespace clang; -using namespace clang::cxstring; +using namespace clang::cxindex; //===----------------------------------------------------------------------===// // Internal predicates on CXSourceLocations. @@ -116,40 +118,41 @@ CXSourceLocation clang_getRangeEnd(CXSourceRange range) { extern "C" { -CXSourceLocation clang_getLocation(CXTranslationUnit tu, +CXSourceLocation clang_getLocation(CXTranslationUnit TU, CXFile file, unsigned line, unsigned column) { - if (!tu || !file) + if (!TU || !file) return clang_getNullLocation(); - bool Logging = ::getenv("LIBCLANG_LOGGING"); - ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); + LogRef Log = Logger::make(LLVM_FUNCTION_NAME); + ASTUnit *CXXUnit = cxtu::getASTUnit(TU); ASTUnit::ConcurrencyCheck Check(*CXXUnit); const FileEntry *File = static_cast<const FileEntry *>(file); SourceLocation SLoc = CXXUnit->getLocation(File, line, column); if (SLoc.isInvalid()) { - if (Logging) - llvm::errs() << "clang_getLocation(\"" << File->getName() - << "\", " << line << ", " << column << ") = invalid\n"; + if (Log) + *Log << llvm::format("(\"%s\", %d, %d) = invalid", + File->getName(), line, column); return clang_getNullLocation(); } - if (Logging) - llvm::errs() << "clang_getLocation(\"" << File->getName() - << "\", " << line << ", " << column << ") = " - << SLoc.getRawEncoding() << "\n"; + CXSourceLocation CXLoc = + cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); + if (Log) + *Log << llvm::format("(\"%s\", %d, %d) = ", File->getName(), line, column) + << CXLoc; - return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); + return CXLoc; } -CXSourceLocation clang_getLocationForOffset(CXTranslationUnit tu, +CXSourceLocation clang_getLocationForOffset(CXTranslationUnit TU, CXFile file, unsigned offset) { - if (!tu || !file) + if (!TU || !file) return clang_getNullLocation(); - ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); + ASTUnit *CXXUnit = cxtu::getASTUnit(TU); SourceLocation SLoc = CXXUnit->getLocation(static_cast<const FileEntry *>(file), offset); @@ -183,7 +186,7 @@ static void createNullLocation(CXFile *file, unsigned *line, static void createNullLocation(CXString *filename, unsigned *line, unsigned *column, unsigned *offset = 0) { if (filename) - *filename = createCXString(""); + *filename = cxstring::createEmpty(); if (line) *line = 0; if (column) @@ -228,7 +231,7 @@ void clang_getExpansionLocation(CXSourceLocation location, } if (file) - *file = (void *)SM.getFileEntryForSLocEntry(sloc); + *file = const_cast<FileEntry *>(SM.getFileEntryForSLocEntry(sloc)); if (line) *line = SM.getExpansionLineNumber(ExpansionLoc); if (column) @@ -259,7 +262,7 @@ void clang_getPresumedLocation(CXSourceLocation location, PresumedLoc PreLoc = SM.getPresumedLoc(Loc); if (filename) - *filename = createCXString(PreLoc.getFilename()); + *filename = cxstring::createRef(PreLoc.getFilename()); if (line) *line = PreLoc.getLine(); if (column) @@ -295,16 +298,8 @@ void clang_getSpellingLocation(CXSourceLocation location, const SourceManager &SM = *static_cast<const SourceManager*>(location.ptr_data[0]); - SourceLocation SpellLoc = Loc; - if (SpellLoc.isMacroID()) { - SourceLocation SimpleSpellingLoc = SM.getImmediateSpellingLoc(SpellLoc); - if (SimpleSpellingLoc.isFileID() && - SM.getFileEntryForID(SM.getDecomposedLoc(SimpleSpellingLoc).first)) - SpellLoc = SimpleSpellingLoc; - else - SpellLoc = SM.getExpansionLoc(SpellLoc); - } - + // FIXME: This should call SourceManager::getSpellingLoc(). + SourceLocation SpellLoc = SM.getFileLoc(Loc); std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc); FileID FID = LocInfo.first; unsigned FileOffset = LocInfo.second; @@ -313,7 +308,7 @@ void clang_getSpellingLocation(CXSourceLocation location, return createNullLocation(file, line, column, offset); if (file) - *file = (void *)SM.getFileEntryForID(FID); + *file = const_cast<FileEntry *>(SM.getFileEntryForID(FID)); if (line) *line = SM.getLineNumber(FID, FileOffset); if (column) @@ -322,5 +317,41 @@ void clang_getSpellingLocation(CXSourceLocation location, *offset = FileOffset; } -} // end extern "C" +void clang_getFileLocation(CXSourceLocation location, + CXFile *file, + unsigned *line, + unsigned *column, + unsigned *offset) { + + if (!isASTUnitSourceLocation(location)) { + CXLoadedDiagnostic::decodeLocation(location, file, line, + column, offset); + return; + } + + SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); + + if (!location.ptr_data[0] || Loc.isInvalid()) + return createNullLocation(file, line, column, offset); + + const SourceManager &SM = + *static_cast<const SourceManager*>(location.ptr_data[0]); + SourceLocation FileLoc = SM.getFileLoc(Loc); + std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(FileLoc); + FileID FID = LocInfo.first; + unsigned FileOffset = LocInfo.second; + if (FID.isInvalid()) + return createNullLocation(file, line, column, offset); + + if (file) + *file = const_cast<FileEntry *>(SM.getFileEntryForID(FID)); + if (line) + *line = SM.getLineNumber(FID, FileOffset); + if (column) + *column = SM.getColumnNumber(FID, FileOffset); + if (offset) + *offset = FileOffset; +} + +} // end extern "C" |