| 1 |
//===- ExpansionPolicy.hh - Declare header expansion policy ---- *- 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 |
/// Implements a Expansion Policy for #include headers. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
/* Author: Giuliano Belinassi */ |
| 15 |
|
| 16 |
#pragma once |
| 17 |
|
| 18 |
#include <llvm/ADT/StringRef.h> |
| 19 |
#include <memory> |
| 20 |
|
| 21 |
using namespace llvm; |
| 22 |
|
| 23 |
class IncludeExpansionPolicy |
| 24 |
{ |
| 25 |
public: |
| 26 |
virtual bool Must_Expand(const StringRef &absolute_path, const StringRef &relative_path) = 0; |
| 27 |
|
| 28 |
virtual bool Must_Not_Expand(const StringRef &absolute_path, const StringRef &relative_path) = 0; |
| 29 |
|
| 30 |
|
| 31 |
virtual ~IncludeExpansionPolicy() = default; |
| 32 |
|
| 33 |
enum Policy { |
| 34 |
INVALID, |
| 35 |
NOTHING, |
| 36 |
EVERYTHING, |
| 37 |
KERNEL, |
| 38 |
SYSTEM, |
| 39 |
COMPILER, |
| 40 |
}; |
| 41 |
|
| 42 |
static std::unique_ptr<IncludeExpansionPolicy> |
| 43 |
Get_Expansion_Policy_Unique(Policy policy); |
| 44 |
|
| 45 |
/* Return if headers passed through -include must be expanded. */ |
| 46 |
static bool Expand_Minus_Includes(Policy policy); |
| 47 |
|
| 48 |
static Policy Get_From_String(const char *string); |
| 49 |
inline static Policy Get_Overriding(const char *string, bool is_kernel) |
| 50 |
{ |
| 51 |
/* In case a policy name wasn't specified (string is null), then override |
| 52 |
acoriding to the following guess. */ |
| 53 |
Policy p = Get_From_String(string); |
| 54 |
if (p == Policy::INVALID) { |
| 55 |
if (is_kernel) { |
| 56 |
return Policy::KERNEL; |
| 57 |
} else { |
| 58 |
return Policy::NOTHING; |
| 59 |
} |
| 60 |
} |
| 61 |
return p; |
| 62 |
} |
| 63 |
protected: |
| 64 |
}; |
| 65 |
|
| 66 |
class NoIncludeExpansionPolicy : public IncludeExpansionPolicy |
| 67 |
{ |
| 68 |
public: |
| 69 |
virtual bool Must_Expand(const StringRef &absolute_path, const StringRef &relative_path) |
| 70 |
{ |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
virtual bool Must_Not_Expand(const StringRef &absolute_path, const StringRef &relative_path) |
| 75 |
{ |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
}; |
| 80 |
|
| 81 |
class ExpandEverythingExpansionPolicy : public IncludeExpansionPolicy |
| 82 |
{ |
| 83 |
public: |
| 84 |
virtual bool Must_Expand(const StringRef &absolute_path, const StringRef &relative_path) |
| 85 |
{ |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
virtual bool Must_Not_Expand(const StringRef &absolute_path, const StringRef &relative_path) |
| 90 |
{ |
| 91 |
return false; |
| 92 |
} |
| 93 |
}; |
| 94 |
|
| 95 |
/** Expand any header according to kernel livepatching rules. */ |
| 96 |
class KernelExpansionPolicy : public IncludeExpansionPolicy |
| 97 |
{ |
| 98 |
public: |
| 99 |
virtual bool Must_Expand(const StringRef &absolute_path, const StringRef &relative_path); |
| 100 |
|
| 101 |
virtual bool Must_Not_Expand(const StringRef &absolute_path, const StringRef &relative_path) |
| 102 |
{ |
| 103 |
return false; |
| 104 |
} |
| 105 |
}; |
| 106 |
|
| 107 |
/** Expand any header that is not installed in the system. */ |
| 108 |
class SystemExpansionPolicy : public IncludeExpansionPolicy |
| 109 |
{ |
| 110 |
public: |
| 111 |
virtual bool Must_Expand(const StringRef &absolute_path, const StringRef &relative_path); |
| 112 |
|
| 113 |
virtual bool Must_Not_Expand(const StringRef &absolute_path, const StringRef &relative_path) |
| 114 |
{ |
| 115 |
return false; |
| 116 |
} |
| 117 |
}; |
| 118 |
|
| 119 |
/** Expand any header that is not compiler-specific. */ |
| 120 |
class CompilerExpansionPolicy : public IncludeExpansionPolicy |
| 121 |
{ |
| 122 |
public: |
| 123 |
virtual bool Must_Expand(const StringRef &absolute_path, const StringRef &relative_path); |
| 124 |
virtual bool Must_Not_Expand(const StringRef &absolute_path, const StringRef &relative_path); |
| 125 |
}; |