1 //===-- LVSupport.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 // This implements the supporting functions.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/DebugInfo/LogicalView/Core/LVSupport.h"
14 #include "llvm/Support/FormatAdapters.h"
15 #include "llvm/Support/FormatVariadic.h"
16 #include <iomanip>
17
18 using namespace llvm;
19 using namespace llvm::logicalview;
20
21 #define DEBUG_TYPE "Support"
22
23 namespace {
24 // Unique string pool instance used by all logical readers.
25 LVStringPool StringPool;
26 } // namespace
getStringPool()27 LVStringPool &llvm::logicalview::getStringPool() { return StringPool; }
28
29 // Perform the following transformations to the given 'Path':
30 // - all characters to lowercase.
31 // - '\\' into '/' (Platform independent).
32 // - '//' into '/'
transformPath(StringRef Path)33 std::string llvm::logicalview::transformPath(StringRef Path) {
34 std::string Name(Path);
35 std::transform(Name.begin(), Name.end(), Name.begin(), tolower);
36 std::replace(Name.begin(), Name.end(), '\\', '/');
37
38 // Remove all duplicate slashes.
39 size_t Pos = 0;
40 while ((Pos = Name.find("//", Pos)) != std::string::npos)
41 Name.erase(Pos, 1);
42
43 return Name;
44 }
45
46 // Convert the given 'Path' to lowercase and change any matching character
47 // from 'CharSet' into '_'.
48 // The characters in 'CharSet' are:
49 // '/', '\', '<', '>', '.', ':', '%', '*', '?', '|', '"', ' '.
flattenedFilePath(StringRef Path)50 std::string llvm::logicalview::flattenedFilePath(StringRef Path) {
51 std::string Name(Path);
52 std::transform(Name.begin(), Name.end(), Name.begin(), tolower);
53
54 const char *CharSet = "/\\<>.:%*?|\" ";
55 char *Input = Name.data();
56 while (Input && *Input) {
57 Input = strpbrk(Input, CharSet);
58 if (Input)
59 *Input++ = '_';
60 };
61 return Name;
62 }
63