Skip to content

Commit 3d2efe7

Browse files
authored
use Twine instead of char* for function args (llvm#165569)
Changed the function arguments to take `const Twine&` instead of `const char*`. This will avoid converting StringRef's to C strings too soon (or ever).
1 parent a4ffa1f commit 3d2efe7

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

clang/lib/Basic/SourceManager.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,7 @@ FileID SourceManager::createFileIDImpl(ContentCache &File, StringRef Filename,
608608
return FileID::get(LoadedID);
609609
}
610610
unsigned FileSize = File.getSize();
611-
llvm::ErrorOr<bool> NeedConversion =
612-
llvm::needConversion(Filename.str().c_str());
611+
llvm::ErrorOr<bool> NeedConversion = llvm::needConversion(Filename);
613612
if (NeedConversion && *NeedConversion) {
614613
// Buffer size may increase due to potential z/OS EBCDIC to UTF-8
615614
// conversion.

llvm/include/llvm/Support/AutoConvert.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <_Ccsid.h>
1919
#endif
2020
#ifdef __cplusplus
21+
#include "llvm/ADT/Twine.h"
2122
#include "llvm/Support/Error.h"
2223
#include <system_error>
2324
#endif /* __cplusplus */
@@ -47,12 +48,12 @@ namespace llvm {
4748
std::error_code setzOSFileTag(int FD, int CCSID, bool Text);
4849

4950
/** \brief Get the the tag ccsid for a file name or a file descriptor. */
50-
ErrorOr<__ccsid_t> getzOSFileTag(const char *FileName, const int FD = -1);
51+
ErrorOr<__ccsid_t> getzOSFileTag(const Twine &FileName, const int FD = -1);
5152

5253
/** \brief Query the file tag to determine if it needs conversion to UTF-8
5354
* codepage.
5455
*/
55-
ErrorOr<bool> needzOSConversion(const char *FileName, const int FD = -1);
56+
ErrorOr<bool> needzOSConversion(const Twine &FileName, const int FD = -1);
5657

5758
#endif /* __MVS__*/
5859

@@ -87,7 +88,7 @@ inline std::error_code setFileTag(int FD, int CCSID, bool Text) {
8788
return std::error_code();
8889
}
8990

90-
inline ErrorOr<bool> needConversion(const char *FileName, const int FD = -1) {
91+
inline ErrorOr<bool> needConversion(const Twine &FileName, const int FD = -1) {
9192
#ifdef __MVS__
9293
return needzOSConversion(FileName, FD);
9394
#endif

llvm/lib/Support/AutoConvert.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ std::error_code llvm::setzOSFileTag(int FD, int CCSID, bool Text) {
9696
return std::error_code();
9797
}
9898

99-
ErrorOr<__ccsid_t> llvm::getzOSFileTag(const char *FileName, const int FD) {
99+
ErrorOr<__ccsid_t> llvm::getzOSFileTag(const Twine &FileName, const int FD) {
100100
// If we have a file descriptor, use it to find out file tagging. Otherwise we
101101
// need to use stat() with the file path.
102102
if (FD != -1) {
@@ -110,12 +110,12 @@ ErrorOr<__ccsid_t> llvm::getzOSFileTag(const char *FileName, const int FD) {
110110
return Query.fccsid;
111111
}
112112
struct stat Attr;
113-
if (stat(FileName, &Attr) == -1)
113+
if (stat(FileName.str().c_str(), &Attr) == -1)
114114
return std::error_code(errno, std::generic_category());
115115
return Attr.st_tag.ft_ccsid;
116116
}
117117

118-
ErrorOr<bool> llvm::needzOSConversion(const char *FileName, const int FD) {
118+
ErrorOr<bool> llvm::needzOSConversion(const Twine &FileName, const int FD) {
119119
ErrorOr<__ccsid_t> Ccsid = getzOSFileTag(FileName, FD);
120120
if (std::error_code EC = Ccsid.getError())
121121
return EC;

llvm/lib/Support/MemoryBuffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
512512
}
513513

514514
#ifdef __MVS__
515-
ErrorOr<bool> NeedsConversion = needConversion(Filename.str().c_str(), FD);
515+
ErrorOr<bool> NeedsConversion = needConversion(Filename, FD);
516516
if (std::error_code EC = NeedsConversion.getError())
517517
return EC;
518518
// File size may increase due to EBCDIC -> UTF-8 conversion, therefore we

0 commit comments

Comments
 (0)