xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Basic/SourceLocation.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===- SourceLocation.cpp - Compact identifier for Source Files -----------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg //  This file defines accessor methods for the FullSourceLoc class.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg 
137330f729Sjoerg #include "clang/Basic/SourceLocation.h"
147330f729Sjoerg #include "clang/Basic/LLVM.h"
157330f729Sjoerg #include "clang/Basic/PrettyStackTrace.h"
167330f729Sjoerg #include "clang/Basic/SourceManager.h"
17*e038c9c4Sjoerg #include "llvm/ADT/DenseMapInfo.h"
18*e038c9c4Sjoerg #include "llvm/ADT/FoldingSet.h"
197330f729Sjoerg #include "llvm/ADT/StringRef.h"
207330f729Sjoerg #include "llvm/Support/Compiler.h"
217330f729Sjoerg #include "llvm/Support/MemoryBuffer.h"
227330f729Sjoerg #include "llvm/Support/raw_ostream.h"
237330f729Sjoerg #include <cassert>
247330f729Sjoerg #include <string>
257330f729Sjoerg #include <utility>
267330f729Sjoerg 
277330f729Sjoerg using namespace clang;
287330f729Sjoerg 
297330f729Sjoerg //===----------------------------------------------------------------------===//
307330f729Sjoerg // PrettyStackTraceLoc
317330f729Sjoerg //===----------------------------------------------------------------------===//
327330f729Sjoerg 
print(raw_ostream & OS) const337330f729Sjoerg void PrettyStackTraceLoc::print(raw_ostream &OS) const {
347330f729Sjoerg   if (Loc.isValid()) {
357330f729Sjoerg     Loc.print(OS, SM);
367330f729Sjoerg     OS << ": ";
377330f729Sjoerg   }
387330f729Sjoerg   OS << Message << '\n';
397330f729Sjoerg }
407330f729Sjoerg 
417330f729Sjoerg //===----------------------------------------------------------------------===//
427330f729Sjoerg // SourceLocation
437330f729Sjoerg //===----------------------------------------------------------------------===//
447330f729Sjoerg 
45*e038c9c4Sjoerg static_assert(std::is_trivially_destructible<SourceLocation>::value,
46*e038c9c4Sjoerg               "SourceLocation must be trivially destructible because it is "
47*e038c9c4Sjoerg               "used in unions");
48*e038c9c4Sjoerg 
49*e038c9c4Sjoerg static_assert(std::is_trivially_destructible<SourceRange>::value,
50*e038c9c4Sjoerg               "SourceRange must be trivially destructible because it is "
51*e038c9c4Sjoerg               "used in unions");
52*e038c9c4Sjoerg 
getHashValue() const53*e038c9c4Sjoerg unsigned SourceLocation::getHashValue() const {
54*e038c9c4Sjoerg   return llvm::DenseMapInfo<unsigned>::getHashValue(ID);
55*e038c9c4Sjoerg }
56*e038c9c4Sjoerg 
Profile(const SourceLocation & X,llvm::FoldingSetNodeID & ID)57*e038c9c4Sjoerg void llvm::FoldingSetTrait<SourceLocation>::Profile(
58*e038c9c4Sjoerg     const SourceLocation &X, llvm::FoldingSetNodeID &ID) {
59*e038c9c4Sjoerg   ID.AddInteger(X.ID);
60*e038c9c4Sjoerg }
61*e038c9c4Sjoerg 
print(raw_ostream & OS,const SourceManager & SM) const627330f729Sjoerg void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{
637330f729Sjoerg   if (!isValid()) {
647330f729Sjoerg     OS << "<invalid loc>";
657330f729Sjoerg     return;
667330f729Sjoerg   }
677330f729Sjoerg 
687330f729Sjoerg   if (isFileID()) {
697330f729Sjoerg     PresumedLoc PLoc = SM.getPresumedLoc(*this);
707330f729Sjoerg 
717330f729Sjoerg     if (PLoc.isInvalid()) {
727330f729Sjoerg       OS << "<invalid>";
737330f729Sjoerg       return;
747330f729Sjoerg     }
757330f729Sjoerg     // The macro expansion and spelling pos is identical for file locs.
767330f729Sjoerg     OS << PLoc.getFilename() << ':' << PLoc.getLine()
777330f729Sjoerg        << ':' << PLoc.getColumn();
787330f729Sjoerg     return;
797330f729Sjoerg   }
807330f729Sjoerg 
817330f729Sjoerg   SM.getExpansionLoc(*this).print(OS, SM);
827330f729Sjoerg 
837330f729Sjoerg   OS << " <Spelling=";
847330f729Sjoerg   SM.getSpellingLoc(*this).print(OS, SM);
857330f729Sjoerg   OS << '>';
867330f729Sjoerg }
877330f729Sjoerg 
887330f729Sjoerg LLVM_DUMP_METHOD std::string
printToString(const SourceManager & SM) const897330f729Sjoerg SourceLocation::printToString(const SourceManager &SM) const {
907330f729Sjoerg   std::string S;
917330f729Sjoerg   llvm::raw_string_ostream OS(S);
927330f729Sjoerg   print(OS, SM);
937330f729Sjoerg   return OS.str();
947330f729Sjoerg }
957330f729Sjoerg 
dump(const SourceManager & SM) const967330f729Sjoerg LLVM_DUMP_METHOD void SourceLocation::dump(const SourceManager &SM) const {
977330f729Sjoerg   print(llvm::errs(), SM);
987330f729Sjoerg   llvm::errs() << '\n';
997330f729Sjoerg }
1007330f729Sjoerg 
dump(const SourceManager & SM) const1017330f729Sjoerg LLVM_DUMP_METHOD void SourceRange::dump(const SourceManager &SM) const {
1027330f729Sjoerg   print(llvm::errs(), SM);
1037330f729Sjoerg   llvm::errs() << '\n';
1047330f729Sjoerg }
1057330f729Sjoerg 
PrintDifference(raw_ostream & OS,const SourceManager & SM,SourceLocation Loc,PresumedLoc Previous)1067330f729Sjoerg static PresumedLoc PrintDifference(raw_ostream &OS, const SourceManager &SM,
1077330f729Sjoerg                                    SourceLocation Loc, PresumedLoc Previous) {
1087330f729Sjoerg   if (Loc.isFileID()) {
1097330f729Sjoerg 
1107330f729Sjoerg     PresumedLoc PLoc = SM.getPresumedLoc(Loc);
1117330f729Sjoerg 
1127330f729Sjoerg     if (PLoc.isInvalid()) {
1137330f729Sjoerg       OS << "<invalid sloc>";
1147330f729Sjoerg       return Previous;
1157330f729Sjoerg     }
1167330f729Sjoerg 
1177330f729Sjoerg     if (Previous.isInvalid() ||
1187330f729Sjoerg         strcmp(PLoc.getFilename(), Previous.getFilename()) != 0) {
1197330f729Sjoerg       OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':'
1207330f729Sjoerg          << PLoc.getColumn();
1217330f729Sjoerg     } else if (Previous.isInvalid() || PLoc.getLine() != Previous.getLine()) {
1227330f729Sjoerg       OS << "line" << ':' << PLoc.getLine() << ':' << PLoc.getColumn();
1237330f729Sjoerg     } else {
1247330f729Sjoerg       OS << "col" << ':' << PLoc.getColumn();
1257330f729Sjoerg     }
1267330f729Sjoerg     return PLoc;
1277330f729Sjoerg   }
1287330f729Sjoerg   auto PrintedLoc = PrintDifference(OS, SM, SM.getExpansionLoc(Loc), Previous);
1297330f729Sjoerg 
1307330f729Sjoerg   OS << " <Spelling=";
1317330f729Sjoerg   PrintedLoc = PrintDifference(OS, SM, SM.getSpellingLoc(Loc), PrintedLoc);
1327330f729Sjoerg   OS << '>';
1337330f729Sjoerg   return PrintedLoc;
1347330f729Sjoerg }
1357330f729Sjoerg 
print(raw_ostream & OS,const SourceManager & SM) const1367330f729Sjoerg void SourceRange::print(raw_ostream &OS, const SourceManager &SM) const {
1377330f729Sjoerg 
1387330f729Sjoerg   OS << '<';
1397330f729Sjoerg   auto PrintedLoc = PrintDifference(OS, SM, B, {});
1407330f729Sjoerg   if (B != E) {
1417330f729Sjoerg     OS << ", ";
1427330f729Sjoerg     PrintDifference(OS, SM, E, PrintedLoc);
1437330f729Sjoerg   }
1447330f729Sjoerg   OS << '>';
1457330f729Sjoerg }
1467330f729Sjoerg 
1477330f729Sjoerg LLVM_DUMP_METHOD std::string
printToString(const SourceManager & SM) const1487330f729Sjoerg SourceRange::printToString(const SourceManager &SM) const {
1497330f729Sjoerg   std::string S;
1507330f729Sjoerg   llvm::raw_string_ostream OS(S);
1517330f729Sjoerg   print(OS, SM);
1527330f729Sjoerg   return OS.str();
1537330f729Sjoerg }
1547330f729Sjoerg 
1557330f729Sjoerg //===----------------------------------------------------------------------===//
1567330f729Sjoerg // FullSourceLoc
1577330f729Sjoerg //===----------------------------------------------------------------------===//
1587330f729Sjoerg 
getFileID() const1597330f729Sjoerg FileID FullSourceLoc::getFileID() const {
1607330f729Sjoerg   assert(isValid());
1617330f729Sjoerg   return SrcMgr->getFileID(*this);
1627330f729Sjoerg }
1637330f729Sjoerg 
getExpansionLoc() const1647330f729Sjoerg FullSourceLoc FullSourceLoc::getExpansionLoc() const {
1657330f729Sjoerg   assert(isValid());
1667330f729Sjoerg   return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
1677330f729Sjoerg }
1687330f729Sjoerg 
getSpellingLoc() const1697330f729Sjoerg FullSourceLoc FullSourceLoc::getSpellingLoc() const {
1707330f729Sjoerg   assert(isValid());
1717330f729Sjoerg   return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
1727330f729Sjoerg }
1737330f729Sjoerg 
getFileLoc() const1747330f729Sjoerg FullSourceLoc FullSourceLoc::getFileLoc() const {
1757330f729Sjoerg   assert(isValid());
1767330f729Sjoerg   return FullSourceLoc(SrcMgr->getFileLoc(*this), *SrcMgr);
1777330f729Sjoerg }
1787330f729Sjoerg 
getPresumedLoc(bool UseLineDirectives) const1797330f729Sjoerg PresumedLoc FullSourceLoc::getPresumedLoc(bool UseLineDirectives) const {
1807330f729Sjoerg   if (!isValid())
1817330f729Sjoerg     return PresumedLoc();
1827330f729Sjoerg 
1837330f729Sjoerg   return SrcMgr->getPresumedLoc(*this, UseLineDirectives);
1847330f729Sjoerg }
1857330f729Sjoerg 
isMacroArgExpansion(FullSourceLoc * StartLoc) const1867330f729Sjoerg bool FullSourceLoc::isMacroArgExpansion(FullSourceLoc *StartLoc) const {
1877330f729Sjoerg   assert(isValid());
1887330f729Sjoerg   return SrcMgr->isMacroArgExpansion(*this, StartLoc);
1897330f729Sjoerg }
1907330f729Sjoerg 
getImmediateMacroCallerLoc() const1917330f729Sjoerg FullSourceLoc FullSourceLoc::getImmediateMacroCallerLoc() const {
1927330f729Sjoerg   assert(isValid());
1937330f729Sjoerg   return FullSourceLoc(SrcMgr->getImmediateMacroCallerLoc(*this), *SrcMgr);
1947330f729Sjoerg }
1957330f729Sjoerg 
getModuleImportLoc() const1967330f729Sjoerg std::pair<FullSourceLoc, StringRef> FullSourceLoc::getModuleImportLoc() const {
1977330f729Sjoerg   if (!isValid())
1987330f729Sjoerg     return std::make_pair(FullSourceLoc(), StringRef());
1997330f729Sjoerg 
2007330f729Sjoerg   std::pair<SourceLocation, StringRef> ImportLoc =
2017330f729Sjoerg       SrcMgr->getModuleImportLoc(*this);
2027330f729Sjoerg   return std::make_pair(FullSourceLoc(ImportLoc.first, *SrcMgr),
2037330f729Sjoerg                         ImportLoc.second);
2047330f729Sjoerg }
2057330f729Sjoerg 
getFileOffset() const2067330f729Sjoerg unsigned FullSourceLoc::getFileOffset() const {
2077330f729Sjoerg   assert(isValid());
2087330f729Sjoerg   return SrcMgr->getFileOffset(*this);
2097330f729Sjoerg }
2107330f729Sjoerg 
getLineNumber(bool * Invalid) const2117330f729Sjoerg unsigned FullSourceLoc::getLineNumber(bool *Invalid) const {
2127330f729Sjoerg   assert(isValid());
2137330f729Sjoerg   return SrcMgr->getLineNumber(getFileID(), getFileOffset(), Invalid);
2147330f729Sjoerg }
2157330f729Sjoerg 
getColumnNumber(bool * Invalid) const2167330f729Sjoerg unsigned FullSourceLoc::getColumnNumber(bool *Invalid) const {
2177330f729Sjoerg   assert(isValid());
2187330f729Sjoerg   return SrcMgr->getColumnNumber(getFileID(), getFileOffset(), Invalid);
2197330f729Sjoerg }
2207330f729Sjoerg 
getFileEntry() const2217330f729Sjoerg const FileEntry *FullSourceLoc::getFileEntry() const {
2227330f729Sjoerg   assert(isValid());
2237330f729Sjoerg   return SrcMgr->getFileEntryForID(getFileID());
2247330f729Sjoerg }
2257330f729Sjoerg 
getExpansionLineNumber(bool * Invalid) const2267330f729Sjoerg unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
2277330f729Sjoerg   assert(isValid());
2287330f729Sjoerg   return SrcMgr->getExpansionLineNumber(*this, Invalid);
2297330f729Sjoerg }
2307330f729Sjoerg 
getExpansionColumnNumber(bool * Invalid) const2317330f729Sjoerg unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
2327330f729Sjoerg   assert(isValid());
2337330f729Sjoerg   return SrcMgr->getExpansionColumnNumber(*this, Invalid);
2347330f729Sjoerg }
2357330f729Sjoerg 
getSpellingLineNumber(bool * Invalid) const2367330f729Sjoerg unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
2377330f729Sjoerg   assert(isValid());
2387330f729Sjoerg   return SrcMgr->getSpellingLineNumber(*this, Invalid);
2397330f729Sjoerg }
2407330f729Sjoerg 
getSpellingColumnNumber(bool * Invalid) const2417330f729Sjoerg unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
2427330f729Sjoerg   assert(isValid());
2437330f729Sjoerg   return SrcMgr->getSpellingColumnNumber(*this, Invalid);
2447330f729Sjoerg }
2457330f729Sjoerg 
isInSystemHeader() const2467330f729Sjoerg bool FullSourceLoc::isInSystemHeader() const {
2477330f729Sjoerg   assert(isValid());
2487330f729Sjoerg   return SrcMgr->isInSystemHeader(*this);
2497330f729Sjoerg }
2507330f729Sjoerg 
isBeforeInTranslationUnitThan(SourceLocation Loc) const2517330f729Sjoerg bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
2527330f729Sjoerg   assert(isValid());
2537330f729Sjoerg   return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
2547330f729Sjoerg }
2557330f729Sjoerg 
dump() const2567330f729Sjoerg LLVM_DUMP_METHOD void FullSourceLoc::dump() const {
2577330f729Sjoerg   SourceLocation::dump(*SrcMgr);
2587330f729Sjoerg }
2597330f729Sjoerg 
getCharacterData(bool * Invalid) const2607330f729Sjoerg const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
2617330f729Sjoerg   assert(isValid());
2627330f729Sjoerg   return SrcMgr->getCharacterData(*this, Invalid);
2637330f729Sjoerg }
2647330f729Sjoerg 
getBufferData(bool * Invalid) const2657330f729Sjoerg StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
2667330f729Sjoerg   assert(isValid());
267*e038c9c4Sjoerg   return SrcMgr->getBufferData(SrcMgr->getFileID(*this), Invalid);
2687330f729Sjoerg }
2697330f729Sjoerg 
getDecomposedLoc() const2707330f729Sjoerg std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
2717330f729Sjoerg   return SrcMgr->getDecomposedLoc(*this);
2727330f729Sjoerg }
273