1193323Sed//===- Support/FileUtilities.cpp - File System Utilities ------------------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file implements a family of utility functions which are useful for doing
11193323Sed// various things with files.
12193323Sed//
13193323Sed//===----------------------------------------------------------------------===//
14193323Sed
15193323Sed#include "llvm/Support/FileUtilities.h"
16249423Sdim#include "llvm/ADT/OwningPtr.h"
17249423Sdim#include "llvm/ADT/SmallString.h"
18203954Srdivacky#include "llvm/Support/MemoryBuffer.h"
19249423Sdim#include "llvm/Support/Path.h"
20203954Srdivacky#include "llvm/Support/raw_ostream.h"
21218893Sdim#include "llvm/Support/system_error.h"
22249423Sdim#include <cctype>
23193323Sed#include <cstdlib>
24193323Sed#include <cstring>
25193323Sedusing namespace llvm;
26193323Sed
27193323Sedstatic bool isSignedChar(char C) {
28193323Sed  return (C == '+' || C == '-');
29193323Sed}
30193323Sed
31193323Sedstatic bool isExponentChar(char C) {
32193323Sed  switch (C) {
33193323Sed  case 'D':  // Strange exponential notation.
34193323Sed  case 'd':  // Strange exponential notation.
35193323Sed  case 'e':
36193323Sed  case 'E': return true;
37193323Sed  default: return false;
38193323Sed  }
39193323Sed}
40193323Sed
41193323Sedstatic bool isNumberChar(char C) {
42193323Sed  switch (C) {
43193323Sed  case '0': case '1': case '2': case '3': case '4':
44193323Sed  case '5': case '6': case '7': case '8': case '9':
45193323Sed  case '.': return true;
46193323Sed  default: return isSignedChar(C) || isExponentChar(C);
47193323Sed  }
48193323Sed}
49193323Sed
50193323Sedstatic const char *BackupNumber(const char *Pos, const char *FirstChar) {
51193323Sed  // If we didn't stop in the middle of a number, don't backup.
52193323Sed  if (!isNumberChar(*Pos)) return Pos;
53193323Sed
54193323Sed  // Otherwise, return to the start of the number.
55210299Sed  bool HasPeriod = false;
56193323Sed  while (Pos > FirstChar && isNumberChar(Pos[-1])) {
57210299Sed    // Backup over at most one period.
58210299Sed    if (Pos[-1] == '.') {
59210299Sed      if (HasPeriod)
60210299Sed        break;
61210299Sed      HasPeriod = true;
62210299Sed    }
63210299Sed
64193323Sed    --Pos;
65193323Sed    if (Pos > FirstChar && isSignedChar(Pos[0]) && !isExponentChar(Pos[-1]))
66193323Sed      break;
67193323Sed  }
68193323Sed  return Pos;
69193323Sed}
70193323Sed
71193323Sed/// EndOfNumber - Return the first character that is not part of the specified
72193323Sed/// number.  This assumes that the buffer is null terminated, so it won't fall
73193323Sed/// off the end.
74193323Sedstatic const char *EndOfNumber(const char *Pos) {
75193323Sed  while (isNumberChar(*Pos))
76193323Sed    ++Pos;
77193323Sed  return Pos;
78193323Sed}
79193323Sed
80193323Sed/// CompareNumbers - compare two numbers, returning true if they are different.
81193323Sedstatic bool CompareNumbers(const char *&F1P, const char *&F2P,
82193323Sed                           const char *F1End, const char *F2End,
83193323Sed                           double AbsTolerance, double RelTolerance,
84193323Sed                           std::string *ErrorMsg) {
85193323Sed  const char *F1NumEnd, *F2NumEnd;
86193323Sed  double V1 = 0.0, V2 = 0.0;
87193323Sed
88193323Sed  // If one of the positions is at a space and the other isn't, chomp up 'til
89193323Sed  // the end of the space.
90249423Sdim  while (isspace(static_cast<unsigned char>(*F1P)) && F1P != F1End)
91193323Sed    ++F1P;
92249423Sdim  while (isspace(static_cast<unsigned char>(*F2P)) && F2P != F2End)
93193323Sed    ++F2P;
94193323Sed
95193323Sed  // If we stop on numbers, compare their difference.
96193323Sed  if (!isNumberChar(*F1P) || !isNumberChar(*F2P)) {
97193323Sed    // The diff failed.
98193323Sed    F1NumEnd = F1P;
99193323Sed    F2NumEnd = F2P;
100193323Sed  } else {
101193323Sed    // Note that some ugliness is built into this to permit support for numbers
102193323Sed    // that use "D" or "d" as their exponential marker, e.g. "1.234D45".  This
103193323Sed    // occurs in 200.sixtrack in spec2k.
104193323Sed    V1 = strtod(F1P, const_cast<char**>(&F1NumEnd));
105193323Sed    V2 = strtod(F2P, const_cast<char**>(&F2NumEnd));
106193323Sed
107193323Sed    if (*F1NumEnd == 'D' || *F1NumEnd == 'd') {
108193323Sed      // Copy string into tmp buffer to replace the 'D' with an 'e'.
109193323Sed      SmallString<200> StrTmp(F1P, EndOfNumber(F1NumEnd)+1);
110193323Sed      // Strange exponential notation!
111193323Sed      StrTmp[static_cast<unsigned>(F1NumEnd-F1P)] = 'e';
112218893Sdim
113193323Sed      V1 = strtod(&StrTmp[0], const_cast<char**>(&F1NumEnd));
114193323Sed      F1NumEnd = F1P + (F1NumEnd-&StrTmp[0]);
115193323Sed    }
116218893Sdim
117193323Sed    if (*F2NumEnd == 'D' || *F2NumEnd == 'd') {
118193323Sed      // Copy string into tmp buffer to replace the 'D' with an 'e'.
119193323Sed      SmallString<200> StrTmp(F2P, EndOfNumber(F2NumEnd)+1);
120193323Sed      // Strange exponential notation!
121193323Sed      StrTmp[static_cast<unsigned>(F2NumEnd-F2P)] = 'e';
122218893Sdim
123193323Sed      V2 = strtod(&StrTmp[0], const_cast<char**>(&F2NumEnd));
124193323Sed      F2NumEnd = F2P + (F2NumEnd-&StrTmp[0]);
125193323Sed    }
126193323Sed  }
127193323Sed
128193323Sed  if (F1NumEnd == F1P || F2NumEnd == F2P) {
129193323Sed    if (ErrorMsg) {
130193323Sed      *ErrorMsg = "FP Comparison failed, not a numeric difference between '";
131193323Sed      *ErrorMsg += F1P[0];
132193323Sed      *ErrorMsg += "' and '";
133193323Sed      *ErrorMsg += F2P[0];
134193323Sed      *ErrorMsg += "'";
135193323Sed    }
136193323Sed    return true;
137193323Sed  }
138193323Sed
139193323Sed  // Check to see if these are inside the absolute tolerance
140193323Sed  if (AbsTolerance < std::abs(V1-V2)) {
141193323Sed    // Nope, check the relative tolerance...
142193323Sed    double Diff;
143193323Sed    if (V2)
144193323Sed      Diff = std::abs(V1/V2 - 1.0);
145193323Sed    else if (V1)
146193323Sed      Diff = std::abs(V2/V1 - 1.0);
147193323Sed    else
148193323Sed      Diff = 0;  // Both zero.
149193323Sed    if (Diff > RelTolerance) {
150193323Sed      if (ErrorMsg) {
151203954Srdivacky        raw_string_ostream(*ErrorMsg)
152203954Srdivacky          << "Compared: " << V1 << " and " << V2 << '\n'
153203954Srdivacky          << "abs. diff = " << std::abs(V1-V2) << " rel.diff = " << Diff << '\n'
154203954Srdivacky          << "Out of tolerance: rel/abs: " << RelTolerance << '/'
155203954Srdivacky          << AbsTolerance;
156193323Sed      }
157193323Sed      return true;
158193323Sed    }
159193323Sed  }
160193323Sed
161193323Sed  // Otherwise, advance our read pointers to the end of the numbers.
162193323Sed  F1P = F1NumEnd;  F2P = F2NumEnd;
163193323Sed  return false;
164193323Sed}
165193323Sed
166193323Sed/// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
167193323Sed/// files match, 1 if they are different, and 2 if there is a file error.  This
168193323Sed/// function differs from DiffFiles in that you can specify an absolete and
169193323Sed/// relative FP error that is allowed to exist.  If you specify a string to fill
170193323Sed/// in for the error option, it will set the string to an error message if an
171193323Sed/// error occurs, allowing the caller to distinguish between a failed diff and a
172193323Sed/// file system error.
173193323Sed///
174263508Sdimint llvm::DiffFilesWithTolerance(StringRef NameA,
175263508Sdim                                 StringRef NameB,
176193323Sed                                 double AbsTol, double RelTol,
177193323Sed                                 std::string *Error) {
178221345Sdim  // Now its safe to mmap the files into memory because both files
179193323Sed  // have a non-zero size.
180218893Sdim  OwningPtr<MemoryBuffer> F1;
181263508Sdim  if (error_code ec = MemoryBuffer::getFile(NameA, F1)) {
182218893Sdim    if (Error)
183218893Sdim      *Error = ec.message();
184193323Sed    return 2;
185218893Sdim  }
186218893Sdim  OwningPtr<MemoryBuffer> F2;
187263508Sdim  if (error_code ec = MemoryBuffer::getFile(NameB, F2)) {
188218893Sdim    if (Error)
189218893Sdim      *Error = ec.message();
190218893Sdim    return 2;
191218893Sdim  }
192218893Sdim
193193323Sed  // Okay, now that we opened the files, scan them for the first difference.
194193323Sed  const char *File1Start = F1->getBufferStart();
195193323Sed  const char *File2Start = F2->getBufferStart();
196193323Sed  const char *File1End = F1->getBufferEnd();
197193323Sed  const char *File2End = F2->getBufferEnd();
198193323Sed  const char *F1P = File1Start;
199193323Sed  const char *F2P = File2Start;
200263508Sdim  uint64_t A_size = F1->getBufferSize();
201263508Sdim  uint64_t B_size = F2->getBufferSize();
202193323Sed
203210299Sed  // Are the buffers identical?  Common case: Handle this efficiently.
204210299Sed  if (A_size == B_size &&
205210299Sed      std::memcmp(File1Start, File2Start, A_size) == 0)
206210299Sed    return 0;
207193323Sed
208210299Sed  // Otherwise, we are done a tolerances are set.
209210299Sed  if (AbsTol == 0 && RelTol == 0) {
210210299Sed    if (Error)
211210299Sed      *Error = "Files differ without tolerance allowance";
212210299Sed    return 1;   // Files different!
213193323Sed  }
214193323Sed
215193323Sed  bool CompareFailed = false;
216193323Sed  while (1) {
217193323Sed    // Scan for the end of file or next difference.
218193323Sed    while (F1P < File1End && F2P < File2End && *F1P == *F2P)
219193323Sed      ++F1P, ++F2P;
220193323Sed
221193323Sed    if (F1P >= File1End || F2P >= File2End) break;
222193323Sed
223193323Sed    // Okay, we must have found a difference.  Backup to the start of the
224193323Sed    // current number each stream is at so that we can compare from the
225193323Sed    // beginning.
226193323Sed    F1P = BackupNumber(F1P, File1Start);
227193323Sed    F2P = BackupNumber(F2P, File2Start);
228193323Sed
229193323Sed    // Now that we are at the start of the numbers, compare them, exiting if
230193323Sed    // they don't match.
231193323Sed    if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
232193323Sed      CompareFailed = true;
233193323Sed      break;
234193323Sed    }
235193323Sed  }
236193323Sed
237193323Sed  // Okay, we reached the end of file.  If both files are at the end, we
238193323Sed  // succeeded.
239193323Sed  bool F1AtEnd = F1P >= File1End;
240193323Sed  bool F2AtEnd = F2P >= File2End;
241193323Sed  if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
242193323Sed    // Else, we might have run off the end due to a number: backup and retry.
243193323Sed    if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
244193323Sed    if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
245193323Sed    F1P = BackupNumber(F1P, File1Start);
246193323Sed    F2P = BackupNumber(F2P, File2Start);
247193323Sed
248193323Sed    // Now that we are at the start of the numbers, compare them, exiting if
249193323Sed    // they don't match.
250193323Sed    if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
251193323Sed      CompareFailed = true;
252193323Sed
253193323Sed    // If we found the end, we succeeded.
254193323Sed    if (F1P < File1End || F2P < File2End)
255193323Sed      CompareFailed = true;
256193323Sed  }
257193323Sed
258193323Sed  return CompareFailed;
259193323Sed}
260