blob: 5236bfaffba2bb461400ae15141885e1d7d70b94 [file] [log] [blame]
Ted Kremenek19a95bc2007-10-25 16:02:43 +00001//==--- SourceLocation.cpp - Compact identifier for Source Files -*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek19a95bc2007-10-25 16:02:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines serialization methods for the SourceLocation class.
Ted Kremenek9c728dc2007-12-12 22:39:36 +000011// This file defines accessor methods for the FullSourceLoc class.
Ted Kremenek19a95bc2007-10-25 16:02:43 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Basic/SourceLocation.h"
Ted Kremenek9c728dc2007-12-12 22:39:36 +000016#include "clang/Basic/SourceManager.h"
Ted Kremenek19a95bc2007-10-25 16:02:43 +000017#include "llvm/Bitcode/Serialize.h"
18#include "llvm/Bitcode/Deserialize.h"
Ted Kremenek19a95bc2007-10-25 16:02:43 +000019using namespace clang;
20
Ted Kremenekbeb77132007-11-01 22:25:41 +000021void SourceLocation::Emit(llvm::Serializer& S) const {
22 S.EmitInt(getRawEncoding());
Ted Kremenek19a95bc2007-10-25 16:02:43 +000023}
24
Ted Kremenekbeb77132007-11-01 22:25:41 +000025SourceLocation SourceLocation::ReadVal(llvm::Deserializer& D) {
Ted Kremenek19a95bc2007-10-25 16:02:43 +000026 return SourceLocation::getFromRawEncoding(D.ReadInt());
27}
Ted Kremenekbeb77132007-11-01 22:25:41 +000028
29void SourceRange::Emit(llvm::Serializer& S) const {
30 B.Emit(S);
31 E.Emit(S);
32}
33
34SourceRange SourceRange::ReadVal(llvm::Deserializer& D) {
35 SourceLocation A = SourceLocation::ReadVal(D);
36 SourceLocation B = SourceLocation::ReadVal(D);
37 return SourceRange(A,B);
38}
Ted Kremenek9c728dc2007-12-12 22:39:36 +000039
Chris Lattner5c38b632008-09-29 21:46:13 +000040FullSourceLoc FullSourceLoc::getLogicalLoc() const {
Ted Kremenek9c728dc2007-12-12 22:39:36 +000041 assert (isValid());
Chris Lattner5c38b632008-09-29 21:46:13 +000042 return FullSourceLoc(SrcMgr->getLogicalLoc(Loc), *SrcMgr);
Ted Kremenek9c728dc2007-12-12 22:39:36 +000043}
44
Chris Lattner5c38b632008-09-29 21:46:13 +000045FullSourceLoc FullSourceLoc::getPhysicalLoc() const {
Ted Kremenek9c728dc2007-12-12 22:39:36 +000046 assert (isValid());
Chris Lattner5c38b632008-09-29 21:46:13 +000047 return FullSourceLoc(SrcMgr->getPhysicalLoc(Loc), *SrcMgr);
48}
49
50FullSourceLoc FullSourceLoc::getIncludeLoc() const {
51 assert (isValid());
52 return FullSourceLoc(SrcMgr->getIncludeLoc(Loc), *SrcMgr);
Ted Kremenek9c728dc2007-12-12 22:39:36 +000053}
54
Ted Kremenek1758b072008-04-03 17:55:15 +000055unsigned FullSourceLoc::getLineNumber() const {
Ted Kremenek9c728dc2007-12-12 22:39:36 +000056 assert (isValid());
57 return SrcMgr->getLineNumber(Loc);
58}
59
Ted Kremenek1758b072008-04-03 17:55:15 +000060unsigned FullSourceLoc::getColumnNumber() const {
Ted Kremenek9c728dc2007-12-12 22:39:36 +000061 assert (isValid());
62 return SrcMgr->getColumnNumber(Loc);
63}
64
Ted Kremenek1758b072008-04-03 17:55:15 +000065
66unsigned FullSourceLoc::getLogicalLineNumber() const {
67 assert (isValid());
68 return SrcMgr->getLogicalLineNumber(Loc);
69}
70
71unsigned FullSourceLoc::getLogicalColumnNumber() const {
72 assert (isValid());
73 return SrcMgr->getLogicalColumnNumber(Loc);
74}
75
Chris Lattner5c38b632008-09-29 21:46:13 +000076unsigned FullSourceLoc::getPhysicalLineNumber() const {
77 assert (isValid());
78 return SrcMgr->getPhysicalLineNumber(Loc);
79}
80
81unsigned FullSourceLoc::getPhysicalColumnNumber() const {
82 assert (isValid());
83 return SrcMgr->getPhysicalColumnNumber(Loc);
84}
85
Ted Kremenek9c728dc2007-12-12 22:39:36 +000086const char* FullSourceLoc::getSourceName() const {
87 assert (isValid());
88 return SrcMgr->getSourceName(Loc);
89}
90
91const FileEntry* FullSourceLoc::getFileEntryForLoc() const {
92 assert (isValid());
93 return SrcMgr->getFileEntryForLoc(Loc);
94}
95
Nico Weber7bfaaae2008-08-10 19:59:06 +000096bool FullSourceLoc::isInSystemHeader() const {
97 assert (isValid());
98 return SrcMgr->isInSystemHeader(Loc);
99}
100
101
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000102const char * FullSourceLoc::getCharacterData() const {
103 assert (isValid());
104 return SrcMgr->getCharacterData(Loc);
105}
106
107const llvm::MemoryBuffer* FullSourceLoc::getBuffer() const {
108 assert (isValid());
109 return SrcMgr->getBuffer(Loc.getFileID());
110}
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000111
112unsigned FullSourceLoc::getCanonicalFileID() const {
113 return SrcMgr->getCanonicalFileID(Loc);
114}
Chris Lattner5c38b632008-09-29 21:46:13 +0000115
116void FullSourceLoc::dump() const {
117 if (!isValid()) {
118 fprintf(stderr, "Invalid Loc\n");
119 return;
120 }
121
122 if (isFileID()) {
123 // The logical and physical pos is identical for file locs.
124 fprintf(stderr, "File Loc from '%s': %d: %d\n",
125 getSourceName(), getLogicalLineNumber(),
126 getLogicalColumnNumber());
127 } else {
128 fprintf(stderr, "Macro Loc (\n Physical: ");
129 getPhysicalLoc().dump();
130 fprintf(stderr, " Logical: ");
131 getLogicalLoc().dump();
132 fprintf(stderr, ")\n");
133 }
134}