~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/lib/Support/SourceMgr.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===- SourceMgr.cpp - Manager for Simple Source Buffers & Diagnostics ----===//
 
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 implements the SourceMgr class.  This class is used as a simple
 
11
// substrate for diagnostics, #include handling, and other low level things for
 
12
// simple parsers.
 
13
//
 
14
//===----------------------------------------------------------------------===//
 
15
 
 
16
#include "llvm/Support/SourceMgr.h"
 
17
#include "llvm/Support/MemoryBuffer.h"
 
18
#include "llvm/Support/raw_ostream.h"
 
19
using namespace llvm;
 
20
 
 
21
namespace {
 
22
  struct LineNoCacheTy {
 
23
    int LastQueryBufferID;
 
24
    const char *LastQuery;
 
25
    unsigned LineNoOfQuery;
 
26
  };
 
27
}
 
28
 
 
29
static LineNoCacheTy *getCache(void *Ptr) {
 
30
  return (LineNoCacheTy*)Ptr;
 
31
}
 
32
 
 
33
 
 
34
SourceMgr::~SourceMgr() {
 
35
  // Delete the line # cache if allocated.
 
36
  if (LineNoCacheTy *Cache = getCache(LineNoCache))
 
37
    delete Cache;
 
38
 
 
39
  while (!Buffers.empty()) {
 
40
    delete Buffers.back().Buffer;
 
41
    Buffers.pop_back();
 
42
  }
 
43
}
 
44
 
 
45
/// AddIncludeFile - Search for a file with the specified name in the current
 
46
/// directory or in one of the IncludeDirs.  If no file is found, this returns
 
47
/// ~0, otherwise it returns the buffer ID of the stacked file.
 
48
unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
 
49
                                   SMLoc IncludeLoc) {
 
50
 
 
51
  MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str());
 
52
 
 
53
  // If the file didn't exist directly, see if it's in an include path.
 
54
  for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
 
55
    std::string IncFile = IncludeDirectories[i] + "/" + Filename;
 
56
    NewBuf = MemoryBuffer::getFile(IncFile.c_str());
 
57
  }
 
58
 
 
59
  if (NewBuf == 0) return ~0U;
 
60
 
 
61
  return AddNewSourceBuffer(NewBuf, IncludeLoc);
 
62
}
 
63
 
 
64
 
 
65
/// FindBufferContainingLoc - Return the ID of the buffer containing the
 
66
/// specified location, returning -1 if not found.
 
67
int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
 
68
  for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
 
69
    if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
 
70
        // Use <= here so that a pointer to the null at the end of the buffer
 
71
        // is included as part of the buffer.
 
72
        Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
 
73
      return i;
 
74
  return -1;
 
75
}
 
76
 
 
77
/// FindLineNumber - Find the line number for the specified location in the
 
78
/// specified file.  This is not a fast method.
 
79
unsigned SourceMgr::FindLineNumber(SMLoc Loc, int BufferID) const {
 
80
  if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
 
81
  assert(BufferID != -1 && "Invalid Location!");
 
82
 
 
83
  MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer;
 
84
 
 
85
  // Count the number of \n's between the start of the file and the specified
 
86
  // location.
 
87
  unsigned LineNo = 1;
 
88
 
 
89
  const char *Ptr = Buff->getBufferStart();
 
90
 
 
91
  // If we have a line number cache, and if the query is to a later point in the
 
92
  // same file, start searching from the last query location.  This optimizes
 
93
  // for the case when multiple diagnostics come out of one file in order.
 
94
  if (LineNoCacheTy *Cache = getCache(LineNoCache))
 
95
    if (Cache->LastQueryBufferID == BufferID &&
 
96
        Cache->LastQuery <= Loc.getPointer()) {
 
97
      Ptr = Cache->LastQuery;
 
98
      LineNo = Cache->LineNoOfQuery;
 
99
    }
 
100
 
 
101
  // Scan for the location being queried, keeping track of the number of lines
 
102
  // we see.
 
103
  for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
 
104
    if (*Ptr == '\n') ++LineNo;
 
105
 
 
106
 
 
107
  // Allocate the line number cache if it doesn't exist.
 
108
  if (LineNoCache == 0)
 
109
    LineNoCache = new LineNoCacheTy();
 
110
 
 
111
  // Update the line # cache.
 
112
  LineNoCacheTy &Cache = *getCache(LineNoCache);
 
113
  Cache.LastQueryBufferID = BufferID;
 
114
  Cache.LastQuery = Ptr;
 
115
  Cache.LineNoOfQuery = LineNo;
 
116
  return LineNo;
 
117
}
 
118
 
 
119
void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
 
120
  if (IncludeLoc == SMLoc()) return;  // Top of stack.
 
121
 
 
122
  int CurBuf = FindBufferContainingLoc(IncludeLoc);
 
123
  assert(CurBuf != -1 && "Invalid or unspecified location!");
 
124
 
 
125
  PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
 
126
 
 
127
  OS << "Included from "
 
128
     << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
 
129
     << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
 
130
}
 
131
 
 
132
 
 
133
/// GetMessage - Return an SMDiagnostic at the specified location with the
 
134
/// specified string.
 
135
///
 
136
/// @param Type - If non-null, the kind of message (e.g., "error") which is
 
137
/// prefixed to the message.
 
138
SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, const std::string &Msg,
 
139
                                   const char *Type, bool ShowLine) const {
 
140
 
 
141
  // First thing to do: find the current buffer containing the specified
 
142
  // location.
 
143
  int CurBuf = FindBufferContainingLoc(Loc);
 
144
  assert(CurBuf != -1 && "Invalid or unspecified location!");
 
145
 
 
146
  MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
 
147
 
 
148
  // Scan backward to find the start of the line.
 
149
  const char *LineStart = Loc.getPointer();
 
150
  while (LineStart != CurMB->getBufferStart() &&
 
151
         LineStart[-1] != '\n' && LineStart[-1] != '\r')
 
152
    --LineStart;
 
153
 
 
154
  std::string LineStr;
 
155
  if (ShowLine) {
 
156
    // Get the end of the line.
 
157
    const char *LineEnd = Loc.getPointer();
 
158
    while (LineEnd != CurMB->getBufferEnd() &&
 
159
           LineEnd[0] != '\n' && LineEnd[0] != '\r')
 
160
      ++LineEnd;
 
161
    LineStr = std::string(LineStart, LineEnd);
 
162
  }
 
163
 
 
164
  std::string PrintedMsg;
 
165
  if (Type) {
 
166
    PrintedMsg = Type;
 
167
    PrintedMsg += ": ";
 
168
  }
 
169
  PrintedMsg += Msg;
 
170
 
 
171
  return SMDiagnostic(CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf),
 
172
                      Loc.getPointer()-LineStart, PrintedMsg,
 
173
                      LineStr, ShowLine);
 
174
}
 
175
 
 
176
void SourceMgr::PrintMessage(SMLoc Loc, const std::string &Msg,
 
177
                             const char *Type, bool ShowLine) const {
 
178
  raw_ostream &OS = errs();
 
179
 
 
180
  int CurBuf = FindBufferContainingLoc(Loc);
 
181
  assert(CurBuf != -1 && "Invalid or unspecified location!");
 
182
  PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
 
183
 
 
184
  GetMessage(Loc, Msg, Type, ShowLine).Print(0, OS);
 
185
}
 
186
 
 
187
//===----------------------------------------------------------------------===//
 
188
// SMDiagnostic Implementation
 
189
//===----------------------------------------------------------------------===//
 
190
 
 
191
void SMDiagnostic::Print(const char *ProgName, raw_ostream &S) const {
 
192
  if (ProgName && ProgName[0])
 
193
    S << ProgName << ": ";
 
194
 
 
195
  if (!Filename.empty()) {
 
196
    if (Filename == "-")
 
197
      S << "<stdin>";
 
198
    else
 
199
      S << Filename;
 
200
 
 
201
    if (LineNo != -1) {
 
202
      S << ':' << LineNo;
 
203
      if (ColumnNo != -1)
 
204
        S << ':' << (ColumnNo+1);
 
205
    }
 
206
    S << ": ";
 
207
  }
 
208
 
 
209
  S << Message << '\n';
 
210
 
 
211
  if (LineNo != -1 && ColumnNo != -1 && ShowLine) {
 
212
    S << LineContents << '\n';
 
213
 
 
214
    // Print out spaces/tabs before the caret.
 
215
    for (unsigned i = 0; i != unsigned(ColumnNo); ++i)
 
216
      S << (LineContents[i] == '\t' ? '\t' : ' ');
 
217
    S << "^\n";
 
218
  }
 
219
}
 
220
 
 
221