Skip to content

Commit 4feca93

Browse files
committed
Move InputType::Archive and DynObj to SearchDirs::SearchInputType
This commit moves InputType::Archive and InputType::DynObj to a newly created enum SearchDirs::SearchInputType. Input::InputType::Archive and Input::InputType::DynObj were very misleading because we create no inputs of these types. Additionally, one of the other InputType enum value is Namespec, making it theoretically possible for an input to be both Namespec and Archive/DynObj. This is wrong because Type-like enum values should be disjoint.
1 parent c690296 commit 4feca93

4 files changed

Lines changed: 51 additions & 27 deletions

File tree

‎include/eld/Input/SearchDirs.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ class SearchDirs {
4242
typedef DirListType::iterator iterator;
4343
typedef DirListType::const_iterator const_iterator;
4444

45+
/// Input type preference for library search.
46+
enum SearchInputType {
47+
Archive,
48+
DynObj,
49+
Script,
50+
};
51+
4552
public:
4653
SearchDirs(DiagnosticEngine *Diag) : DiagEngine(Diag) {}
4754

@@ -52,7 +59,7 @@ class SearchDirs {
5259

5360
// find - give a namespec, return a real path of the shared object.
5461
const sys::fs::Path *find(const std::string &PNamespec,
55-
Input::InputType PPreferType) const;
62+
SearchInputType PPreferType) const;
5663

5764
const eld::sys::fs::Path *findLibrary(llvm::StringRef Type,
5865
std::string LibraryName,

‎lib/Input/Input.cpp‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ bool Input::resolvePath(const LinkerConfig &PConfig) {
106106
ResolvedPath->append(FileName);
107107
}
108108
if (!llvm::sys::fs::exists(ResolvedPath->native())) {
109-
const sys::fs::Path *P = PSearchDirs.find(FileName, Input::Script);
109+
const sys::fs::Path *P =
110+
PSearchDirs.find(FileName, SearchDirs::SearchInputType::Script);
110111
if (P != nullptr)
111112
ResolvedPath = *P;
112113
}
@@ -115,11 +116,13 @@ bool Input::resolvePath(const LinkerConfig &PConfig) {
115116
const sys::fs::Path *NameSpecPath = nullptr;
116117
if (Attr.isStatic()) {
117118
// with --static, we must search an archive.
118-
NameSpecPath = PSearchDirs.find(FileName, Input::Archive);
119+
NameSpecPath =
120+
PSearchDirs.find(FileName, SearchDirs::SearchInputType::Archive);
119121
} else {
120122
// otherwise, with --Bdynamic, we can find either an archive or a
121123
// shared object.
122-
NameSpecPath = PSearchDirs.find(FileName, Input::DynObj);
124+
NameSpecPath =
125+
PSearchDirs.find(FileName, SearchDirs::SearchInputType::DynObj);
123126
}
124127
if (nullptr == NameSpecPath) {
125128
DiagEngine->raise(Diag::err_cannot_find_namespec) << FileName;

‎lib/Input/SearchDirs.cpp‎

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,23 @@ constexpr inline llvm::StringRef foundString(bool Found) {
4141
return Found ? "found" : "not found";
4242
}
4343

44-
bool checkInputFile(Input::InputType Type, llvm::StringRef NameSpec,
44+
llvm::StringRef toString(SearchDirs::SearchInputType Type) {
45+
switch (Type) {
46+
case SearchDirs::SearchInputType::Archive:
47+
return "archive";
48+
case SearchDirs::SearchInputType::DynObj:
49+
return "dynamic object";
50+
case SearchDirs::SearchInputType::Script:
51+
return "linker script";
52+
}
53+
llvm_unreachable("SearchDirs::SearchInputType out of range");
54+
}
55+
56+
bool checkInputFile(SearchDirs::SearchInputType Type, llvm::StringRef NameSpec,
4557
llvm::StringRef FileName, DiagnosticEngine *DiagEngine) {
4658
bool Found = llvm::sys::fs::exists(FileName);
4759
DiagEngine->raise(Diag::verbose_trying_input_file)
48-
<< FileName << Input::toString(Type) << NameSpec << foundString(Found);
60+
<< FileName << toString(Type) << NameSpec << foundString(Found);
4961
return Found;
5062
}
5163

@@ -64,31 +76,30 @@ bool checkLibraryOrConfigFile(llvm::StringRef Type, llvm::StringRef LibraryName,
6476
// Non-member functions
6577
//===----------------------------------------------------------------------===//
6678
static inline bool SpecToFilename(const std::string &pSpec, std::string &pFile,
67-
uint32_t pType) {
79+
SearchDirs::SearchInputType pType) {
6880
bool StartsWithColon = (pSpec.c_str()[0] == ':');
6981
pFile.clear();
7082
if (StartsWithColon) {
7183
pFile = pSpec.substr(1, pSpec.length());
7284
return true;
7385
}
74-
if (pType == Input::Script) {
86+
if (pType == SearchDirs::SearchInputType::Script) {
7587
pFile = pSpec;
7688
return false;
77-
} else {
78-
pFile = "lib";
79-
pFile += pSpec;
80-
switch (pType) {
81-
case Input::DynObj:
82-
pFile += ".so";
83-
break;
84-
case Input::Archive:
85-
pFile += ".a";
86-
break;
87-
default:
88-
break;
89-
}
90-
return false;
9189
}
90+
pFile = "lib";
91+
pFile += pSpec;
92+
switch (pType) {
93+
case SearchDirs::SearchInputType::DynObj:
94+
pFile += ".so";
95+
break;
96+
case SearchDirs::SearchInputType::Archive:
97+
pFile += ".a";
98+
break;
99+
case SearchDirs::SearchInputType::Script:
100+
llvm_unreachable("Script case already handled above");
101+
}
102+
return false;
92103
}
93104

94105
//===----------------------------------------------------------------------===//
@@ -115,7 +126,7 @@ bool SearchDirs::insert(const sys::fs::Path &PPath) {
115126
}
116127

117128
const eld::sys::fs::Path *SearchDirs::find(const std::string &pNamespec,
118-
Input::InputType pType) const {
129+
SearchInputType pType) const {
119130
std::string File;
120131
bool hasNamespace = false;
121132
hasNamespace = SpecToFilename(pNamespec, File, pType);
@@ -128,11 +139,12 @@ const eld::sys::fs::Path *SearchDirs::find(const std::string &pNamespec,
128139
std::string fileName = dirName + "/" + File;
129140
if (checkInputFile(pType, pNamespec, fileName, DiagEngine))
130141
return make<eld::sys::fs::Path>(fileName);
131-
if (Input::DynObj == pType && !hasNamespace) {
142+
if (SearchDirs::SearchInputType::DynObj == pType && !hasNamespace) {
132143
// we should also try linking with archives if we dont find a DSO
133-
SpecToFilename(pNamespec, fileName, Input::Archive);
144+
SpecToFilename(pNamespec, fileName, SearchDirs::SearchInputType::Archive);
134145
fileName = dirName + "/" + fileName;
135-
if (checkInputFile(Input::Archive, pNamespec, fileName, DiagEngine))
146+
if (checkInputFile(SearchDirs::SearchInputType::Archive, pNamespec,
147+
fileName, DiagEngine))
136148
return make<eld::sys::fs::Path>(fileName);
137149
}
138150
} // end of for

‎lib/Script/ScriptAction.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "eld/Script/ScriptAction.h"
1717
#include "eld/Config/LinkerConfig.h"
1818
#include "eld/Input/LinkerScriptFile.h"
19+
#include "eld/Input/SearchDirs.h"
1920
#include "eld/Support/MsgHandling.h"
2021
#include "llvm/Support/FileSystem.h"
2122

@@ -34,7 +35,8 @@ bool ScriptAction::activate(InputBuilder &PBuilder) {
3435
std::string Path = Name;
3536
auto &SearchDirs = ThisConfig.directories();
3637
if (!llvm::sys::fs::exists(Path)) {
37-
const sys::fs::Path *Res = SearchDirs.find(Path, Input::Script);
38+
const sys::fs::Path *Res =
39+
SearchDirs.find(Path, eld::SearchDirs::SearchInputType::Script);
3840
if (Res == nullptr) {
3941
switch (ScriptFileKind) {
4042
case ScriptFile::LDScript:

0 commit comments

Comments
 (0)