Skip to content

Commit 58d67be

Browse files
committed
Add ClangImporterOptions::getRemappedExtraArgs
This commit adds a function to remap the clang arguments passed during compilation. This is intented to be shared across the Swift compiler and LLDB to apply path remapping for debug info paths.
1 parent b0678ca commit 58d67be

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,9 @@ namespace swift {
771771
DisableOverlayModules,
772772
EnableClangSPI);
773773
}
774+
775+
std::vector<std::string> getRemappedExtraArgs(
776+
std::function<std::string(StringRef)> pathRemapCallback) const;
774777
};
775778

776779
} // end namespace swift

lib/Basic/LangOptions.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,56 @@ DiagnosticBehavior LangOptions::getAccessNoteFailureLimit() const {
408408
}
409409
llvm_unreachable("covered switch");
410410
}
411+
412+
std::vector<std::string> ClangImporterOptions::getRemappedExtraArgs(
413+
std::function<std::string(StringRef)> pathRemapCallback) const {
414+
auto consumeIncludeOption = [](StringRef &arg, StringRef &prefix) {
415+
static StringRef options[] = {"-I",
416+
"-F",
417+
"-fmodule-map-file=",
418+
"-iquote",
419+
"-idirafter",
420+
"-iframeworkwithsysroot",
421+
"-iframework",
422+
"-iprefix",
423+
"-iwithprefixbefore",
424+
"-iwithprefix",
425+
"-isystemafter",
426+
"-isystem",
427+
"-isysroot",
428+
"-ivfsoverlay",
429+
"-working-directory=",
430+
"-working-directory"};
431+
for (StringRef &option : options)
432+
if (arg.consume_front(option)) {
433+
prefix = option;
434+
return true;
435+
}
436+
return false;
437+
};
438+
439+
// true if the previous argument was the dash-option of an option pair
440+
bool remap_next = false;
441+
std::vector<std::string> args;
442+
for (auto A : ExtraArgs) {
443+
StringRef prefix;
444+
StringRef arg(A);
445+
446+
if (remap_next) {
447+
remap_next = false;
448+
args.push_back(pathRemapCallback(arg));
449+
} else if (consumeIncludeOption(arg, prefix)) {
450+
if (arg.empty()) {
451+
// Option pair
452+
remap_next = true;
453+
args.push_back(prefix.str());
454+
} else {
455+
// Combine prefix with remapped path value
456+
args.push_back(prefix.str() + pathRemapCallback(arg));
457+
}
458+
} else {
459+
args.push_back(A);
460+
}
461+
}
462+
return args;
463+
}

unittests/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ handle_gyb_sources(
88
add_swift_unittest(SwiftBasicTests
99
BlotMapVectorTest.cpp
1010
CacheTest.cpp
11+
ClangImporterOptionsTest.cpp
1112
ClusteredBitVectorTest.cpp
1213
DemangleTest.cpp
1314
DiverseStackTest.cpp
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===--- ClangImporterOptionsTest.cpp -------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
#include "swift/Basic/LangOptions.h"
13+
#include "llvm/ADT/StringRef.h"
14+
#include "gtest/gtest.h"
15+
16+
static std::string remap(llvm::StringRef path) { return "remapped"; }
17+
18+
TEST(ClangImporterOptions, nonPathsSkipped) {
19+
std::vector<std::string> args = {"-unmapped", "-another=unmapped"};
20+
swift::ClangImporterOptions options;
21+
options.ExtraArgs = args;
22+
23+
EXPECT_EQ(options.getRemappedExtraArgs(remap), args);
24+
}
25+
26+
TEST(ClangImporterOptions, optionPairs) {
27+
std::vector<std::string> args = {"-unmapped", "-another=unmapped",
28+
"-I", "some/path",
29+
"-ivfsoverlay", "another/path"};
30+
swift::ClangImporterOptions options;
31+
options.ExtraArgs = args;
32+
33+
std::vector<std::string> expected = {"-unmapped", "-another=unmapped",
34+
"-I", "remapped",
35+
"-ivfsoverlay", "remapped"};
36+
EXPECT_EQ(options.getRemappedExtraArgs(remap), expected);
37+
}
38+
39+
TEST(ClangImporterOptions, joinedPaths) {
40+
std::vector<std::string> args = {"-unmapped", "-another=unmapped",
41+
"-Isome/path",
42+
"-working-directory=another/path"};
43+
swift::ClangImporterOptions options;
44+
options.ExtraArgs = args;
45+
46+
std::vector<std::string> expected = {"-unmapped", "-another=unmapped",
47+
"-Iremapped",
48+
"-working-directory=remapped"};
49+
EXPECT_EQ(options.getRemappedExtraArgs(remap), expected);
50+
}

0 commit comments

Comments
 (0)