LLVM: lib/Support/FileUtilities.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

22#include

23#include

24#include

25#include

26#include

27#include <system_error>

28

29using namespace llvm;

30

32 return (C == '+' || C == '-');

33}

34

36 switch (C) {

37 case 'D':

38 case 'd':

39 case 'e':

40 case 'E': return true;

41 default: return false;

42 }

43}

44

46 switch (C) {

47 case '0': case '1': case '2': case '3': case '4':

48 case '5': case '6': case '7': case '8': case '9':

49 case '.': return true;

51 }

52}

53

54static const char *BackupNumber(const char *Pos, const char *FirstChar) {

55

57

58

59 bool HasPeriod = false;

60 while (Pos > FirstChar && isNumberChar(Pos[-1])) {

61

62 if (Pos[-1] == '.') {

63 if (HasPeriod)

64 break;

65 HasPeriod = true;

66 }

67

68 --Pos;

70 break;

71 }

72 return Pos;

73}

74

75

76

77

80 ++Pos;

81 return Pos;

82}

83

84

86 const char *F1End, const char *F2End,

87 double AbsTolerance, double RelTolerance,

88 std::string *ErrorMsg) {

89 const char *F1NumEnd, *F2NumEnd;

90 double V1 = 0.0, V2 = 0.0;

91

92

93

94 while (isSpace(static_cast<unsigned char>(*F1P)) && F1P != F1End)

95 ++F1P;

96 while (isSpace(static_cast<unsigned char>(*F2P)) && F2P != F2End)

97 ++F2P;

98

99

101

102 F1NumEnd = F1P;

103 F2NumEnd = F2P;

104 } else {

105

106

107

108 V1 = strtod(F1P, const_cast<char**>(&F1NumEnd));

109 V2 = strtod(F2P, const_cast<char**>(&F2NumEnd));

110

111 if (*F1NumEnd == 'D' || *F1NumEnd == 'd') {

112

114

115 StrTmp[static_cast<unsigned>(F1NumEnd-F1P)] = 'e';

116

117 V1 = strtod(&StrTmp[0], const_cast<char**>(&F1NumEnd));

118 F1NumEnd = F1P + (F1NumEnd-&StrTmp[0]);

119 }

120

121 if (*F2NumEnd == 'D' || *F2NumEnd == 'd') {

122

124

125 StrTmp[static_cast<unsigned>(F2NumEnd-F2P)] = 'e';

126

127 V2 = strtod(&StrTmp[0], const_cast<char**>(&F2NumEnd));

128 F2NumEnd = F2P + (F2NumEnd-&StrTmp[0]);

129 }

130 }

131

132 if (F1NumEnd == F1P || F2NumEnd == F2P) {

133 if (ErrorMsg) {

134 *ErrorMsg = "FP Comparison failed, not a numeric difference between '";

135 *ErrorMsg += F1P[0];

136 *ErrorMsg += "' and '";

137 *ErrorMsg += F2P[0];

138 *ErrorMsg += "'";

139 }

140 return true;

141 }

142

143

144 if (AbsTolerance < std::abs(V1-V2)) {

145

146 double Diff;

147 if (V2)

148 Diff = std::abs(V1/V2 - 1.0);

149 else if (V1)

150 Diff = std::abs(V2/V1 - 1.0);

151 else

152 Diff = 0;

153 if (Diff > RelTolerance) {

154 if (ErrorMsg) {

156 << "Compared: " << V1 << " and " << V2 << '\n'

157 << "abs. diff = " << std::abs(V1-V2) << " rel.diff = " << Diff << '\n'

158 << "Out of tolerance: rel/abs: " << RelTolerance << '/'

159 << AbsTolerance;

160 }

161 return true;

162 }

163 }

164

165

166 F1P = F1NumEnd; F2P = F2NumEnd;

167 return false;

168}

169

170

171

172

173

174

175

176

177

180 double AbsTol, double RelTol,

181 std::string *Error) {

182

183

185 if (std::error_code EC = F1OrErr.getError()) {

187 *Error = EC.message();

188 return 2;

189 }

191

193 if (std::error_code EC = F2OrErr.getError()) {

195 *Error = EC.message();

196 return 2;

197 }

199

200

205 const char *F1P = File1Start;

206 const char *F2P = File2Start;

209

210

211 if (A_size == B_size &&

212 std::memcmp(File1Start, File2Start, A_size) == 0)

213 return 0;

214

215

216 if (AbsTol == 0 && RelTol == 0) {

218 *Error = "Files differ without tolerance allowance";

219 return 1;

220 }

221

222 bool CompareFailed = false;

223 while (true) {

224

225 while (F1P < File1End && F2P < File2End && *F1P == *F2P) {

226 ++F1P;

227 ++F2P;

228 }

229

230 if (F1P >= File1End || F2P >= File2End) break;

231

232

233

234

237

238

239

240 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {

241 CompareFailed = true;

242 break;

243 }

244 }

245

246

247

248 bool F1AtEnd = F1P >= File1End;

249 bool F2AtEnd = F2P >= File2End;

250 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {

251

256

257

258

260 CompareFailed = true;

261

262

263 if (F1P < File1End || F2P < File2End)

264 CompareFailed = true;

265 }

266

267 return CompareFailed;

268}

269

273

274 if (InputFilename != "-") {

277 } else {

279 }

280

281 return FilePermissionsApplier(InputFilename, Status);

282}

283

286 std::optionalsys::fs::perms OverwritePermissions) {

288

289 if (OverwritePermissions)

290 Status.permissions(*OverwritePermissions);

291

292 int FD = 0;

293

294

295

298

302

303 if (CopyDates)

305 FD, Status.getLastAccessedTime(), Status.getLastModificationTime()))

307

312#ifndef _WIN32

313

316#endif

317

321#ifdef _WIN32

323#else

325#endif

327 }

328

331

333}

Provides ErrorOr smart pointer.

static const char * BackupNumber(const char *Pos, const char *FirstChar)

Definition FileUtilities.cpp:54

static const char * EndOfNumber(const char *Pos)

EndOfNumber - Return the first character that is not part of the specified number.

Definition FileUtilities.cpp:78

static bool isExponentChar(char C)

Definition FileUtilities.cpp:35

static bool isSignedChar(char C)

Definition FileUtilities.cpp:31

static bool isNumberChar(char C)

Definition FileUtilities.cpp:45

static bool CompareNumbers(const char *&F1P, const char *&F2P, const char *F1End, const char *F2End, double AbsTolerance, double RelTolerance, std::string *ErrorMsg)

CompareNumbers - compare two numbers, returning true if they are different.

Definition FileUtilities.cpp:85

static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))

Provides a library for accessing information about this process and other processes on the operating ...

This file defines the SmallString class.

Represents either an error or a value T.

std::error_code getError() const

Lightweight error class with error context and mandatory checking.

static ErrorSuccess success()

Create a success value.

Tagged union holding either a T or a Error.

LLVM_ABI Error apply(StringRef OutputFilename, bool CopyDates=false, std::optional< sys::fs::perms > OverwritePermissions=std::nullopt)

Apply stored permissions to the OutputFilename.

Definition FileUtilities.cpp:284

static LLVM_ABI Expected< FilePermissionsApplier > create(StringRef InputFilename)

Definition FileUtilities.cpp:271

This interface provides simple read-only access to a block of memory, and provides simple methods for...

size_t getBufferSize() const

static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)

Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...

const char * getBufferEnd() const

const char * getBufferStart() const

SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...

StringRef - Represent a constant reference to a string, i.e.

A raw_ostream that writes to an std::string.

static LLVM_ABI std::error_code SafelyCloseFileDescriptor(int FD)

Represents the result of a call to sys::fs::status().

@ C

The default llvm calling convention, compatible with C.

LLVM_ABI unsigned getUmask()

Get file creation mode mask of the process.

@ CD_OpenExisting

CD_OpenExisting - When opening a file:

LLVM_ABI std::error_code changeFileOwnership(int FD, uint32_t Owner, uint32_t Group)

Change ownership of a file.

std::error_code openFileForWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp=CD_CreateAlways, OpenFlags Flags=OF_None, unsigned Mode=0666)

Opens the file with the given name in a write-only or read-write mode, returning its open file descri...

LLVM_ABI std::error_code status(const Twine &path, file_status &result, bool follow=true)

Get file status as if by POSIX stat().

LLVM_ABI std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime, TimePoint<> ModificationTime)

Set the file modification and access time.

LLVM_ABI std::error_code setPermissions(const Twine &Path, perms Permissions)

Set file permissions.

This is an optimization pass for GlobalISel generic memory operations.

Error createFileError(const Twine &F, Error E)

Concatenate a source file path and/or name with an Error.

bool isSpace(char C)

Checks whether character C is whitespace in the "C" locale.

LLVM_ABI int DiffFilesWithTolerance(StringRef FileA, StringRef FileB, double AbsTol, double RelTol, std::string *Error=nullptr)

DiffFilesWithTolerance - Compare the two files specified, returning 0 if the files match,...

Definition FileUtilities.cpp:178