LCOV - code coverage report
Current view: top level - lib/Support - FileUtilities.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 0 89 0.0 %
Date: 2018-10-20 13:21:21 Functions: 0 4 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- Support/FileUtilities.cpp - File System Utilities ------------------===//
       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 a family of utility functions which are useful for doing
      11             : // various things with files.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "llvm/Support/FileUtilities.h"
      16             : #include "llvm/ADT/SmallString.h"
      17             : #include "llvm/Support/ErrorOr.h"
      18             : #include "llvm/Support/MemoryBuffer.h"
      19             : #include "llvm/Support/raw_ostream.h"
      20             : #include <cctype>
      21             : #include <cmath>
      22             : #include <cstdint>
      23             : #include <cstdlib>
      24             : #include <cstring>
      25             : #include <memory>
      26             : #include <system_error>
      27             : 
      28             : using namespace llvm;
      29             : 
      30             : static bool isSignedChar(char C) {
      31           0 :   return (C == '+' || C == '-');
      32             : }
      33             : 
      34             : static bool isExponentChar(char C) {
      35             :   switch (C) {
      36             :   case 'D':  // Strange exponential notation.
      37             :   case 'd':  // Strange exponential notation.
      38             :   case 'e':
      39             :   case 'E': return true;
      40             :   default: return false;
      41             :   }
      42             : }
      43             : 
      44           0 : static bool isNumberChar(char C) {
      45             :   switch (C) {
      46             :   case '0': case '1': case '2': case '3': case '4':
      47             :   case '5': case '6': case '7': case '8': case '9':
      48             :   case '.': return true;
      49           0 :   default: return isSignedChar(C) || isExponentChar(C);
      50             :   }
      51             : }
      52             : 
      53           0 : static const char *BackupNumber(const char *Pos, const char *FirstChar) {
      54             :   // If we didn't stop in the middle of a number, don't backup.
      55           0 :   if (!isNumberChar(*Pos)) return Pos;
      56             : 
      57             :   // Otherwise, return to the start of the number.
      58             :   bool HasPeriod = false;
      59           0 :   while (Pos > FirstChar && isNumberChar(Pos[-1])) {
      60             :     // Backup over at most one period.
      61           0 :     if (Pos[-1] == '.') {
      62           0 :       if (HasPeriod)
      63             :         break;
      64             :       HasPeriod = true;
      65             :     }
      66             : 
      67           0 :     --Pos;
      68           0 :     if (Pos > FirstChar && isSignedChar(Pos[0]) && !isExponentChar(Pos[-1]))
      69             :       break;
      70             :   }
      71             :   return Pos;
      72             : }
      73             : 
      74             : /// EndOfNumber - Return the first character that is not part of the specified
      75             : /// number.  This assumes that the buffer is null terminated, so it won't fall
      76             : /// off the end.
      77             : static const char *EndOfNumber(const char *Pos) {
      78           0 :   while (isNumberChar(*Pos))
      79           0 :     ++Pos;
      80             :   return Pos;
      81             : }
      82             : 
      83             : /// CompareNumbers - compare two numbers, returning true if they are different.
      84           0 : static bool CompareNumbers(const char *&F1P, const char *&F2P,
      85             :                            const char *F1End, const char *F2End,
      86             :                            double AbsTolerance, double RelTolerance,
      87             :                            std::string *ErrorMsg) {
      88             :   const char *F1NumEnd, *F2NumEnd;
      89             :   double V1 = 0.0, V2 = 0.0;
      90             : 
      91             :   // If one of the positions is at a space and the other isn't, chomp up 'til
      92             :   // the end of the space.
      93           0 :   while (isspace(static_cast<unsigned char>(*F1P)) && F1P != F1End)
      94           0 :     ++F1P;
      95           0 :   while (isspace(static_cast<unsigned char>(*F2P)) && F2P != F2End)
      96           0 :     ++F2P;
      97             : 
      98             :   // If we stop on numbers, compare their difference.
      99           0 :   if (!isNumberChar(*F1P) || !isNumberChar(*F2P)) {
     100             :     // The diff failed.
     101           0 :     F1NumEnd = F1P;
     102           0 :     F2NumEnd = F2P;
     103             :   } else {
     104             :     // Note that some ugliness is built into this to permit support for numbers
     105             :     // that use "D" or "d" as their exponential marker, e.g. "1.234D45".  This
     106             :     // occurs in 200.sixtrack in spec2k.
     107           0 :     V1 = strtod(F1P, const_cast<char**>(&F1NumEnd));
     108           0 :     V2 = strtod(F2P, const_cast<char**>(&F2NumEnd));
     109             : 
     110           0 :     if (*F1NumEnd == 'D' || *F1NumEnd == 'd') {
     111             :       // Copy string into tmp buffer to replace the 'D' with an 'e'.
     112           0 :       SmallString<200> StrTmp(F1P, EndOfNumber(F1NumEnd)+1);
     113             :       // Strange exponential notation!
     114           0 :       StrTmp[static_cast<unsigned>(F1NumEnd-F1P)] = 'e';
     115             : 
     116           0 :       V1 = strtod(&StrTmp[0], const_cast<char**>(&F1NumEnd));
     117           0 :       F1NumEnd = F1P + (F1NumEnd-&StrTmp[0]);
     118             :     }
     119             : 
     120           0 :     if (*F2NumEnd == 'D' || *F2NumEnd == 'd') {
     121             :       // Copy string into tmp buffer to replace the 'D' with an 'e'.
     122           0 :       SmallString<200> StrTmp(F2P, EndOfNumber(F2NumEnd)+1);
     123             :       // Strange exponential notation!
     124           0 :       StrTmp[static_cast<unsigned>(F2NumEnd-F2P)] = 'e';
     125             : 
     126           0 :       V2 = strtod(&StrTmp[0], const_cast<char**>(&F2NumEnd));
     127           0 :       F2NumEnd = F2P + (F2NumEnd-&StrTmp[0]);
     128             :     }
     129             :   }
     130             : 
     131           0 :   if (F1NumEnd == F1P || F2NumEnd == F2P) {
     132           0 :     if (ErrorMsg) {
     133             :       *ErrorMsg = "FP Comparison failed, not a numeric difference between '";
     134           0 :       *ErrorMsg += F1P[0];
     135             :       *ErrorMsg += "' and '";
     136           0 :       *ErrorMsg += F2P[0];
     137             :       *ErrorMsg += "'";
     138             :     }
     139           0 :     return true;
     140             :   }
     141             : 
     142             :   // Check to see if these are inside the absolute tolerance
     143           0 :   if (AbsTolerance < std::abs(V1-V2)) {
     144             :     // Nope, check the relative tolerance...
     145             :     double Diff;
     146           0 :     if (V2)
     147           0 :       Diff = std::abs(V1/V2 - 1.0);
     148           0 :     else if (V1)
     149           0 :       Diff = std::abs(V2/V1 - 1.0);
     150             :     else
     151             :       Diff = 0;  // Both zero.
     152           0 :     if (Diff > RelTolerance) {
     153           0 :       if (ErrorMsg) {
     154           0 :         raw_string_ostream(*ErrorMsg)
     155           0 :           << "Compared: " << V1 << " and " << V2 << '\n'
     156           0 :           << "abs. diff = " << std::abs(V1-V2) << " rel.diff = " << Diff << '\n'
     157           0 :           << "Out of tolerance: rel/abs: " << RelTolerance << '/'
     158           0 :           << AbsTolerance;
     159             :       }
     160           0 :       return true;
     161             :     }
     162             :   }
     163             : 
     164             :   // Otherwise, advance our read pointers to the end of the numbers.
     165           0 :   F1P = F1NumEnd;  F2P = F2NumEnd;
     166           0 :   return false;
     167             : }
     168             : 
     169             : /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
     170             : /// files match, 1 if they are different, and 2 if there is a file error.  This
     171             : /// function differs from DiffFiles in that you can specify an absolete and
     172             : /// relative FP error that is allowed to exist.  If you specify a string to fill
     173             : /// in for the error option, it will set the string to an error message if an
     174             : /// error occurs, allowing the caller to distinguish between a failed diff and a
     175             : /// file system error.
     176             : ///
     177           0 : int llvm::DiffFilesWithTolerance(StringRef NameA,
     178             :                                  StringRef NameB,
     179             :                                  double AbsTol, double RelTol,
     180             :                                  std::string *Error) {
     181             :   // Now its safe to mmap the files into memory because both files
     182             :   // have a non-zero size.
     183           0 :   ErrorOr<std::unique_ptr<MemoryBuffer>> F1OrErr = MemoryBuffer::getFile(NameA);
     184           0 :   if (std::error_code EC = F1OrErr.getError()) {
     185           0 :     if (Error)
     186           0 :       *Error = EC.message();
     187             :     return 2;
     188             :   }
     189             :   MemoryBuffer &F1 = *F1OrErr.get();
     190             : 
     191           0 :   ErrorOr<std::unique_ptr<MemoryBuffer>> F2OrErr = MemoryBuffer::getFile(NameB);
     192           0 :   if (std::error_code EC = F2OrErr.getError()) {
     193           0 :     if (Error)
     194           0 :       *Error = EC.message();
     195             :     return 2;
     196             :   }
     197             :   MemoryBuffer &F2 = *F2OrErr.get();
     198             : 
     199             :   // Okay, now that we opened the files, scan them for the first difference.
     200           0 :   const char *File1Start = F1.getBufferStart();
     201           0 :   const char *File2Start = F2.getBufferStart();
     202           0 :   const char *File1End = F1.getBufferEnd();
     203           0 :   const char *File2End = F2.getBufferEnd();
     204           0 :   const char *F1P = File1Start;
     205           0 :   const char *F2P = File2Start;
     206             :   uint64_t A_size = F1.getBufferSize();
     207             :   uint64_t B_size = F2.getBufferSize();
     208             : 
     209             :   // Are the buffers identical?  Common case: Handle this efficiently.
     210           0 :   if (A_size == B_size &&
     211           0 :       std::memcmp(File1Start, File2Start, A_size) == 0)
     212             :     return 0;
     213             : 
     214             :   // Otherwise, we are done a tolerances are set.
     215           0 :   if (AbsTol == 0 && RelTol == 0) {
     216           0 :     if (Error)
     217             :       *Error = "Files differ without tolerance allowance";
     218           0 :     return 1;   // Files different!
     219             :   }
     220             : 
     221             :   bool CompareFailed = false;
     222             :   while (true) {
     223             :     // Scan for the end of file or next difference.
     224           0 :     while (F1P < File1End && F2P < File2End && *F1P == *F2P) {
     225           0 :       ++F1P;
     226           0 :       ++F2P;
     227             :     }
     228             : 
     229           0 :     if (F1P >= File1End || F2P >= File2End) break;
     230             : 
     231             :     // Okay, we must have found a difference.  Backup to the start of the
     232             :     // current number each stream is at so that we can compare from the
     233             :     // beginning.
     234           0 :     F1P = BackupNumber(F1P, File1Start);
     235           0 :     F2P = BackupNumber(F2P, File2Start);
     236             : 
     237             :     // Now that we are at the start of the numbers, compare them, exiting if
     238             :     // they don't match.
     239           0 :     if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
     240             :       CompareFailed = true;
     241             :       break;
     242             :     }
     243             :   }
     244             : 
     245             :   // Okay, we reached the end of file.  If both files are at the end, we
     246             :   // succeeded.
     247           0 :   bool F1AtEnd = F1P >= File1End;
     248           0 :   bool F2AtEnd = F2P >= File2End;
     249           0 :   if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
     250             :     // Else, we might have run off the end due to a number: backup and retry.
     251           0 :     if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
     252           0 :     if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
     253           0 :     F1P = BackupNumber(F1P, File1Start);
     254           0 :     F2P = BackupNumber(F2P, File2Start);
     255             : 
     256             :     // Now that we are at the start of the numbers, compare them, exiting if
     257             :     // they don't match.
     258           0 :     if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
     259             :       CompareFailed = true;
     260             : 
     261             :     // If we found the end, we succeeded.
     262           0 :     if (F1P < File1End || F2P < File2End)
     263             :       CompareFailed = true;
     264             :   }
     265             : 
     266           0 :   return CompareFailed;
     267             : }

Generated by: LCOV version 1.13