blob: dcfd547eec4b1e97a93aedd10bdaeafdd6882975 [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 Lattner3b4d5e92009-01-17 08:45:21 +000040FileID FullSourceLoc::getFileID() const {
41 assert(isValid());
42 return SrcMgr->getCanonicalFileID(*this);
43}
44
45
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000046FullSourceLoc FullSourceLoc::getInstantiationLoc() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000047 assert(isValid());
48 return FullSourceLoc(SrcMgr->getInstantiationLoc(*this), *SrcMgr);
Ted Kremenek9c728dc2007-12-12 22:39:36 +000049}
50
Chris Lattnerdf7c17a2009-01-16 07:00:02 +000051FullSourceLoc FullSourceLoc::getSpellingLoc() const {
52 assert(isValid());
Chris Lattnera50bd542009-01-16 23:03:56 +000053 return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
Chris Lattner5c38b632008-09-29 21:46:13 +000054}
55
56FullSourceLoc FullSourceLoc::getIncludeLoc() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000057 assert(isValid());
58 return FullSourceLoc(SrcMgr->getIncludeLoc(*this), *SrcMgr);
Ted Kremenek9c728dc2007-12-12 22:39:36 +000059}
60
Ted Kremenek1758b072008-04-03 17:55:15 +000061unsigned FullSourceLoc::getLineNumber() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000062 assert(isValid());
63 return SrcMgr->getLineNumber(*this);
Ted Kremenek9c728dc2007-12-12 22:39:36 +000064}
65
Ted Kremenek1758b072008-04-03 17:55:15 +000066unsigned FullSourceLoc::getColumnNumber() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000067 assert(isValid());
68 return SrcMgr->getColumnNumber(*this);
Ted Kremenek9c728dc2007-12-12 22:39:36 +000069}
70
Ted Kremenek1758b072008-04-03 17:55:15 +000071
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000072unsigned FullSourceLoc::getInstantiationLineNumber() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000073 assert(isValid());
74 return SrcMgr->getInstantiationLineNumber(*this);
Ted Kremenek1758b072008-04-03 17:55:15 +000075}
76
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000077unsigned FullSourceLoc::getInstantiationColumnNumber() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000078 assert(isValid());
79 return SrcMgr->getInstantiationColumnNumber(*this);
Ted Kremenek1758b072008-04-03 17:55:15 +000080}
81
Chris Lattnerdf7c17a2009-01-16 07:00:02 +000082unsigned FullSourceLoc::getSpellingLineNumber() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000083 assert(isValid());
84 return SrcMgr->getSpellingLineNumber(*this);
Chris Lattner5c38b632008-09-29 21:46:13 +000085}
86
Chris Lattnerdf7c17a2009-01-16 07:00:02 +000087unsigned FullSourceLoc::getSpellingColumnNumber() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000088 assert(isValid());
89 return SrcMgr->getSpellingColumnNumber(*this);
Chris Lattner5c38b632008-09-29 21:46:13 +000090}
91
Ted Kremenek9c728dc2007-12-12 22:39:36 +000092const char* FullSourceLoc::getSourceName() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000093 assert(isValid());
94 return SrcMgr->getSourceName(*this);
Ted Kremenek9c728dc2007-12-12 22:39:36 +000095}
96
97const FileEntry* FullSourceLoc::getFileEntryForLoc() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000098 assert(isValid());
99 return SrcMgr->getFileEntryForLoc(*this);
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000100}
101
Nico Weber7bfaaae2008-08-10 19:59:06 +0000102bool FullSourceLoc::isInSystemHeader() const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000103 assert(isValid());
104 return SrcMgr->isInSystemHeader(*this);
Nico Weber7bfaaae2008-08-10 19:59:06 +0000105}
106
Chris Lattner4abb87e2009-01-16 22:59:51 +0000107const char *FullSourceLoc::getCharacterData() const {
108 assert(isValid());
Chris Lattnera50bd542009-01-16 23:03:56 +0000109 return SrcMgr->getCharacterData(*this);
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000110}
111
112const llvm::MemoryBuffer* FullSourceLoc::getBuffer() const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000113 assert(isValid());
Chris Lattner2b2453a2009-01-17 06:22:33 +0000114 return SrcMgr->getBuffer(*this);
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000115}
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000116
Chris Lattner5c38b632008-09-29 21:46:13 +0000117void FullSourceLoc::dump() const {
118 if (!isValid()) {
119 fprintf(stderr, "Invalid Loc\n");
120 return;
121 }
122
Chris Lattnera50bd542009-01-16 23:03:56 +0000123 if (isFileID()) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000124 // The instantiation and spelling pos is identical for file locs.
Chris Lattner5c38b632008-09-29 21:46:13 +0000125 fprintf(stderr, "File Loc from '%s': %d: %d\n",
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000126 getSourceName(), getInstantiationLineNumber(),
127 getInstantiationColumnNumber());
Chris Lattner5c38b632008-09-29 21:46:13 +0000128 } else {
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000129 fprintf(stderr, "Macro Loc (\n Spelling: ");
130 getSpellingLoc().dump();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000131 fprintf(stderr, " Instantiation: ");
132 getInstantiationLoc().dump();
Chris Lattner5c38b632008-09-29 21:46:13 +0000133 fprintf(stderr, ")\n");
134 }
135}