zcov: / include/clang/Basic/SourceLocation.h


Files: 1 Branches Taken: 55.6% 5 / 9
Generated: 2010-02-10 01:31 Branches Executed: 88.9% 8 / 9
Line Coverage: 100.0% 84 / 84


Programs: 257 Runs 475257


       1                 : //===--- SourceLocation.h - Compact identifier for Source Files -*- C++ -*-===//
       2                 : //
       3                 : //                     The LLVM Compiler Infrastructure
       4                 : //
       5                 : // This file is distributed under the University of Illinois Open Source
       6                 : // License. See LICENSE.TXT for details.
       7                 : //
       8                 : //===----------------------------------------------------------------------===//
       9                 : //
      10                 : //  This file defines the SourceLocation class.
      11                 : //
      12                 : //===----------------------------------------------------------------------===//
      13                 : 
      14                 : #ifndef LLVM_CLANG_SOURCELOCATION_H
      15                 : #define LLVM_CLANG_SOURCELOCATION_H
      16                 : 
      17                 : #include <utility>
      18                 : #include <cassert>
      19                 : 
      20                 : namespace llvm {
      21                 :   class MemoryBuffer;
      22                 :   class raw_ostream;
      23                 :   template <typename T> struct DenseMapInfo;
      24                 :   template <typename T> struct isPodLike;
      25                 : }
      26                 : 
      27                 : namespace clang {
      28                 : 
      29                 : class SourceManager;
      30                 : class FileEntry;
      31                 : 
      32                 : /// FileID - This is an opaque identifier used by SourceManager which refers to
      33                 : /// a source file (MemoryBuffer) along with its #include path and #line data.
      34                 : ///
      35               67: class FileID {
      36                 :   /// ID - Opaque identifier, 0 is "invalid".
      37                 :   unsigned ID;
      38                 : public:
      39          1007992:   FileID() : ID(0) {}
      40                 : 
      41            48167:   bool isInvalid() const { return ID == 0; }
      42                 : 
      43           285822:   bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
      44             1543:   bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
      45                 :   bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
      46             3229:   bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
      47                 :   bool operator>(const FileID &RHS) const { return RHS < *this; }
      48                 :   bool operator>=(const FileID &RHS) const { return RHS <= *this; }
      49                 : 
      50              398:   static FileID getSentinel() { return get(~0U); }
      51              143:   unsigned getHashValue() const { return ID; }
      52                 : 
      53                 : private:
      54                 :   friend class SourceManager;
      55           830733:   static FileID get(unsigned V) {
      56           830733:     FileID F;
      57           830733:     F.ID = V;
      58                 :     return F;
      59                 :   }
      60                 :   unsigned getOpaqueValue() const { return ID; }
      61                 : };
      62                 : 
      63                 : 
      64                 : /// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
      65                 : /// a full include stack, line and column number information for a position in
      66                 : /// an input translation unit.
      67               82: class SourceLocation {
      68                 :   unsigned ID;
      69                 :   friend class SourceManager;
      70                 :   enum {
      71                 :     MacroIDBit = 1U << 31
      72                 :   };
      73                 : public:
      74                 : 
      75         15037854:   SourceLocation() : ID(0) {}  // 0 is an invalid FileID.
      76                 : 
      77          3291050:   bool isFileID() const  { return (ID & MacroIDBit) == 0; }
      78             4475:   bool isMacroID() const { return (ID & MacroIDBit) != 0; }
      79                 : 
      80                 :   /// isValid - Return true if this is a valid SourceLocation object.  Invalid
      81                 :   /// SourceLocations are often used when events have no corresponding location
      82                 :   /// in the source (e.g. a diagnostic is required for a command line option).
      83                 :   ///
      84          2445476:   bool isValid() const { return ID != 0; }
      85           469454:   bool isInvalid() const { return ID == 0; }
      86                 : 
      87                 : private:
      88                 :   /// getOffset - Return the index for SourceManager's SLocEntryTable table,
      89                 :   /// note that this is not an index *into* it though.
      90          4099432:   unsigned getOffset() const {
      91          4099432:     return ID & ~MacroIDBit;
      92                 :   }
      93                 : 
      94            24194:   static SourceLocation getFileLoc(unsigned ID) {
                        0: branch 0 not taken
                    24194: branch 1 taken
      95            24194:     assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
      96            24194:     SourceLocation L;
      97            24194:     L.ID = ID;
      98                 :     return L;
      99                 :   }
     100                 : 
     101            57689:   static SourceLocation getMacroLoc(unsigned ID) {
                    57689: branch 1 taken
     102            57689:     assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
     103            57689:     SourceLocation L;
     104            57689:     L.ID = MacroIDBit | ID;
     105                 :     return L;
     106                 :   }
     107                 : public:
     108                 : 
     109                 :   /// getFileLocWithOffset - Return a source location with the specified offset
     110                 :   /// from this file SourceLocation.
     111          2909865:   SourceLocation getFileLocWithOffset(int Offset) const {
                  2909865: branch 2 taken
                        0: branch 2 not taken
     112          2909865:     assert(((getOffset()+Offset) & MacroIDBit) == 0 && "invalid location");
     113          2909865:     SourceLocation L;
     114          2909865:     L.ID = ID+Offset;
     115                 :     return L;
     116                 :   }
     117                 : 
     118                 :   /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
     119                 :   /// an (opaque) 32-bit integer encoding for it.  This should only be passed
     120                 :   /// to SourceLocation::getFromRawEncoding, it should not be inspected
     121                 :   /// directly.
     122           660798:   unsigned getRawEncoding() const { return ID; }
     123                 : 
     124                 : 
     125                 :   /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
     126                 :   /// a real SourceLocation.
     127            63316:   static SourceLocation getFromRawEncoding(unsigned Encoding) {
     128            63316:     SourceLocation X;
     129            63316:     X.ID = Encoding;
     130                 :     return X;
     131                 :   }
     132                 : 
     133                 :   void print(llvm::raw_ostream &OS, const SourceManager &SM) const;
     134                 :   void dump(const SourceManager &SM) const;
     135                 : };
     136                 : 
     137           188923: inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
     138           188923:   return LHS.getRawEncoding() == RHS.getRawEncoding();
     139                 : }
     140                 : 
     141             1516: inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
     142             1516:   return !(LHS == RHS);
     143                 : }
     144                 : 
     145               47: inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
     146               47:   return LHS.getRawEncoding() < RHS.getRawEncoding();
     147                 : }
     148                 : 
     149                 : /// SourceRange - a trival tuple used to represent a source range.
     150              128: class SourceRange {
     151                 :   SourceLocation B;
     152                 :   SourceLocation E;
     153                 : public:
     154          1860749:   SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
     155           234364:   SourceRange(SourceLocation loc) : B(loc), E(loc) {}
     156           459017:   SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
     157                 : 
     158           519993:   SourceLocation getBegin() const { return B; }
     159           454365:   SourceLocation getEnd() const { return E; }
     160                 : 
     161            88582:   void setBegin(SourceLocation b) { B = b; }
     162           279629:   void setEnd(SourceLocation e) { E = e; }
     163                 : 
                    86985: branch 4 taken
                   803634: branch 5 taken
                        0: branch 5 not taken
                        0: branch 5 not taken
     164          1006197:   bool isValid() const { return B.isValid() && E.isValid(); }
     165           310507:   bool isInvalid() const { return !isValid(); }
     166                 : 
     167             2629:   bool operator==(const SourceRange &X) const {
     168             2629:     return B == X.B && E == X.E;
     169                 :   }
     170                 : 
     171                 :   bool operator!=(const SourceRange &X) const {
     172                 :     return B != X.B || E != X.E;
     173                 :   }
     174                 : };
     175                 : 
     176                 : /// FullSourceLoc - A SourceLocation and its associated SourceManager.  Useful
     177                 : /// for argument passing to functions that expect both objects.
     178                 : class FullSourceLoc : public SourceLocation {
     179                 :   SourceManager* SrcMgr;
     180                 : public:
     181                 :   /// Creates a FullSourceLoc where isValid() returns false.
     182             7938:   explicit FullSourceLoc() : SrcMgr((SourceManager*) 0) {}
     183                 : 
     184            38602:   explicit FullSourceLoc(SourceLocation Loc, SourceManager &SM)
     185            38602:     : SourceLocation(Loc), SrcMgr(&SM) {}
     186                 : 
     187             2126:   SourceManager &getManager() {
     188             2126:     assert(SrcMgr && "SourceManager is NULL.");
     189             2126:     return *SrcMgr;
     190                 :   }
     191                 : 
     192              867:   const SourceManager &getManager() const {
     193              867:     assert(SrcMgr && "SourceManager is NULL.");
     194              867:     return *SrcMgr;
     195                 :   }
     196                 : 
     197                 :   FileID getFileID() const;
     198                 : 
     199                 :   FullSourceLoc getInstantiationLoc() const;
     200                 :   FullSourceLoc getSpellingLoc() const;
     201                 : 
     202                 :   unsigned getInstantiationLineNumber() const;
     203                 :   unsigned getInstantiationColumnNumber() const;
     204                 : 
     205                 :   unsigned getSpellingLineNumber() const;
     206                 :   unsigned getSpellingColumnNumber() const;
     207                 : 
     208                 :   const char *getCharacterData() const;
     209                 : 
     210                 :   const llvm::MemoryBuffer* getBuffer() const;
     211                 : 
     212                 :   /// getBufferData - Return a pointer to the start and end of the source buffer
     213                 :   /// data for the specified FileID.
     214                 :   std::pair<const char*, const char*> getBufferData() const;
     215                 : 
     216                 :   /// getDecomposedLoc - Decompose the specified location into a raw FileID +
     217                 :   /// Offset pair.  The first element is the FileID, the second is the
     218                 :   /// offset from the start of the buffer of the location.
     219                 :   std::pair<FileID, unsigned> getDecomposedLoc() const;
     220                 : 
     221                 :   bool isInSystemHeader() const;
     222                 : 
     223                 :   /// Prints information about this FullSourceLoc to stderr. Useful for
     224                 :   ///  debugging.
     225                 :   void dump() const { SourceLocation::dump(*SrcMgr); }
     226                 : 
     227                 :   friend inline bool
     228             4921:   operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
     229                 :     return LHS.getRawEncoding() == RHS.getRawEncoding() &&
     230             4921:           LHS.SrcMgr == RHS.SrcMgr;
     231                 :   }
     232                 : 
     233                 :   friend inline bool
     234              750:   operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
     235              750:     return !(LHS == RHS);
     236                 :   }
     237                 : 
     238                 : };
     239                 : 
     240                 : /// PresumedLoc - This class represents an unpacked "presumed" location which
     241                 : /// can be presented to the user.  A 'presumed' location can be modified by
     242                 : /// #line and GNU line marker directives and is always the instantiation point
     243                 : /// of a normal location.
     244                 : ///
     245                 : /// You can get a PresumedLoc from a SourceLocation with SourceManager.
     246                 : class PresumedLoc {
     247                 :   const char *Filename;
     248                 :   unsigned Line, Col;
     249                 :   SourceLocation IncludeLoc;
     250                 : public:
     251              470:   PresumedLoc() : Filename(0) {}
     252             9303:   PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
     253             9303:     : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {
     254             9303:   }
     255                 : 
     256                 :   /// isInvalid - Return true if this object is invalid or uninitialized. This
     257                 :   /// occurs when created with invalid source locations or when walking off
     258                 :   /// the top of a #include stack.
     259              542:   bool isInvalid() const { return Filename == 0; }
     260              209:   bool isValid() const { return Filename != 0; }
     261                 : 
     262                 :   /// getFilename - Return the presumed filename of this location.  This can be
     263                 :   /// affected by #line etc.
     264             5515:   const char *getFilename() const { return Filename; }
     265                 : 
     266                 :   /// getLine - Return the presumed line number of this location.  This can be
     267                 :   /// affected by #line etc.
     268             3238:   unsigned getLine() const { return Line; }
     269                 : 
     270                 :   /// getColumn - Return the presumed column number of this location.  This can
     271                 :   /// not be affected by #line, but is packaged here for convenience.
     272             2393:   unsigned getColumn() const { return Col; }
     273                 : 
     274                 :   /// getIncludeLoc - Return the presumed include location of this location.
     275                 :   /// This can be affected by GNU linemarker directives.
     276             4865:   SourceLocation getIncludeLoc() const { return IncludeLoc; }
     277                 : };
     278                 : 
     279                 : 
     280                 : }  // end namespace clang
     281                 : 
     282                 : namespace llvm {
     283                 :   /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
     284                 :   /// DenseSets.
     285                 :   template <>
     286                 :   struct DenseMapInfo<clang::FileID> {
     287              400:     static inline clang::FileID getEmptyKey() {
     288              400:       return clang::FileID();
     289                 :     }
     290              398:     static inline clang::FileID getTombstoneKey() {
     291              398:       return clang::FileID::getSentinel();
     292                 :     }
     293                 : 
     294              133:     static unsigned getHashValue(clang::FileID S) {
     295              133:       return S.getHashValue();
     296                 :     }
     297                 : 
     298              729:     static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
     299              729:       return LHS == RHS;
     300                 :     }
     301                 :   };
     302                 :   
     303                 :   template <>
     304                 :   struct isPodLike<clang::SourceLocation> { static const bool value = true; };
     305                 :   template <>
     306                 :   struct isPodLike<clang::FileID> { static const bool value = true; };
     307                 : 
     308                 : }  // end namespace llvm
     309                 : 
     310                 : #endif

Generated: 2010-02-10 01:31 by zcov