1 //===- Utils.cpp ----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, 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 // Implements utility functions for TextAPI Darwin operations.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/TextAPI/Utils.h"
14 
15 using namespace llvm;
16 using namespace llvm::MachO;
17 
replace_extension(SmallVectorImpl<char> & Path,const Twine & Extension)18 void llvm::MachO::replace_extension(SmallVectorImpl<char> &Path,
19                                     const Twine &Extension) {
20   StringRef P(Path.begin(), Path.size());
21   auto ParentPath = sys::path::parent_path(P);
22   auto Filename = sys::path::filename(P);
23 
24   if (!ParentPath.ends_with(Filename.str() + ".framework")) {
25     sys::path::replace_extension(Path, Extension);
26     return;
27   }
28   // Framework dylibs do not have a file extension, in those cases the new
29   // extension is appended. e.g. given Path: "Foo.framework/Foo" and Extension:
30   // "tbd", the result is "Foo.framework/Foo.tbd".
31   SmallString<8> Storage;
32   StringRef Ext = Extension.toStringRef(Storage);
33 
34   // Append '.' if needed.
35   if (!Ext.empty() && Ext[0] != '.')
36     Path.push_back('.');
37 
38   // Append extension.
39   Path.append(Ext.begin(), Ext.end());
40 }
41