| 1 |
//===- NonLLVMMisc.hh - Implement misc functions that don“t depends on LLVM *- C++ -*-===// |
| 2 |
// |
| 3 |
// This project is licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
/// \file |
| 10 |
/// MISC functions that is used through the program and do not use any LLVM |
| 11 |
/// datastructure. |
| 12 |
// |
| 13 |
//===----------------------------------------------------------------------===// |
| 14 |
|
| 15 |
/* Author: Giuliano Belinassi */ |
| 16 |
|
| 17 |
/* MISC functions that is used through the program but do not use any LLVM |
| 18 |
datastructure. This is put separately because we may want to build tools |
| 19 |
that do not use any LLVM datastructures, and because not including llvm |
| 20 |
headers compiles faster. */ |
| 21 |
|
| 22 |
#pragma once |
| 23 |
|
| 24 |
#include <stdbool.h> |
| 25 |
#include <string.h> |
| 26 |
#include <string> |
| 27 |
#include <vector> |
| 28 |
#include <algorithm> |
| 29 |
|
| 30 |
/** Stringfy constant integer token in `s`. */ |
| 31 |
#define STRINGFY_VALUE(s) STRINGFY(s) |
| 32 |
|
| 33 |
/** Stringfy given expression `s`. */ |
| 34 |
#define STRINGFY(s) #s |
| 35 |
|
| 36 |
/** Handle `free(x)`, where x is not a void* pointer. */ |
| 37 |
#define free(x) free((void*)(x)) |
| 38 |
|
| 39 |
/** Get number of element of array declared statically. */ |
| 40 |
#define ARRAY_LENGTH(v) (sizeof(v)/sizeof(*v)) |
| 41 |
|
| 42 |
/* Check if string a is a prefix of string b. */ |
| 43 |
inline bool prefix(const char *a, const char *b) |
| 44 |
{ |
| 45 |
return !strncmp(a, b, strlen(a)); |
| 46 |
} |
| 47 |
|
| 48 |
/** Check if a string is null or empty. */ |
| 49 |
inline bool is_null_or_empty(const char *str) |
| 50 |
{ |
| 51 |
return str == nullptr || *str == '\0'; |
| 52 |
} |
| 53 |
|
| 54 |
template <typename T> |
| 55 |
void Remove_Duplicates(std::vector<T>& vec) |
| 56 |
{ |
| 57 |
std::sort(vec.begin(), vec.end()); |
| 58 |
vec.erase(std::unique(vec.begin(), vec.end()), vec.end()); |
| 59 |
} |
| 60 |
|
| 61 |
/** Get a single line from a file, removing its newline. |
| 62 |
* |
| 63 |
* NOTE: if this function return a valid pointer, it must be free'd. |
| 64 |
*/ |
| 65 |
char *getline_easy(FILE *file); |
| 66 |
|
| 67 |
/** Check if given path is a directory. */ |
| 68 |
bool Is_Directory(const char *path); |
| 69 |
|
| 70 |
/** Extract arguments that are specified after the '=' sign separated by ','. */ |
| 71 |
std::vector<std::string> Extract_Args(const char *str); |
| 72 |
|
| 73 |
/** Extract argument that are specified after the '=' sign. */ |
| 74 |
std::string Extract_Single_Arg(const char *str); |
| 75 |
|
| 76 |
/** Extract argument that are specified after the '=' sign. */ |
| 77 |
const char *Extract_Single_Arg_C(const char *str); |
| 78 |
|
| 79 |
/** Check if output supports colors. */ |
| 80 |
bool check_color_available(void); |
| 81 |
|
| 82 |
class FileHandling |
| 83 |
{ |
| 84 |
public: |
| 85 |
enum FileType { |
| 86 |
FILE_TYPE_ELF, |
| 87 |
FILE_TYPE_GZ, |
| 88 |
FILE_TYPE_ZSTD, |
| 89 |
FILE_TYPE_UNKNOWN, |
| 90 |
}; |
| 91 |
|
| 92 |
static enum FileType Get_File_Type(int fd); |
| 93 |
}; |
| 94 |
|
| 95 |
/** Get basename of a string. Works like the gnu version. */ |
| 96 |
const char *get_basename(const char *filename); |