File: FileList.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (192 lines) | stat: -rw-r--r-- 6,221 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//===- FileList.cpp ---------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "clang/InstallAPI/FileList.h"
#include "clang/Basic/DiagnosticFrontend.h"
#include "clang/InstallAPI/FileList.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/JSON.h"
#include "llvm/TextAPI/TextAPIError.h"
#include <optional>

// clang-format off
/*
InstallAPI JSON Input Format specification.

{
  "headers" : [                              # Required: Key must exist.
    {                                        # Optional: May contain 0 or more header inputs.
      "path" : "/usr/include/mach-o/dlfn.h", # Required: Path should point to destination
                                             #           location where applicable.
      "type" : "public",                     # Required: Maps to HeaderType for header.
      "language": "c++"                      # Optional: Language mode for header.
    }
  ],
  "version" : "3"                            # Required: Version 3 supports language mode
                                                         & project header input.
}
*/
// clang-format on

using namespace llvm;
using namespace llvm::json;
using namespace llvm::MachO;
using namespace clang::installapi;

namespace {
class Implementation {
private:
  Expected<StringRef> parseString(const Object *Obj, StringRef Key,
                                  StringRef Error);
  Expected<StringRef> parsePath(const Object *Obj);
  Expected<HeaderType> parseType(const Object *Obj);
  std::optional<clang::Language> parseLanguage(const Object *Obj);
  Error parseHeaders(Array &Headers);

public:
  std::unique_ptr<MemoryBuffer> InputBuffer;
  clang::FileManager *FM;
  unsigned Version;
  HeaderSeq HeaderList;

  Error parse(StringRef Input);
};

Expected<StringRef>
Implementation::parseString(const Object *Obj, StringRef Key, StringRef Error) {
  auto Str = Obj->getString(Key);
  if (!Str)
    return make_error<StringError>(Error, inconvertibleErrorCode());
  return *Str;
}

Expected<HeaderType> Implementation::parseType(const Object *Obj) {
  auto TypeStr =
      parseString(Obj, "type", "required field 'type' not specified");
  if (!TypeStr)
    return TypeStr.takeError();

  if (*TypeStr == "public")
    return HeaderType::Public;
  else if (*TypeStr == "private")
    return HeaderType::Private;
  else if (*TypeStr == "project" && Version >= 2)
    return HeaderType::Project;

  return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat,
                                  "unsupported header type");
}

Expected<StringRef> Implementation::parsePath(const Object *Obj) {
  auto Path = parseString(Obj, "path", "required field 'path' not specified");
  if (!Path)
    return Path.takeError();

  return *Path;
}

std::optional<clang::Language>
Implementation::parseLanguage(const Object *Obj) {
  auto Language = Obj->getString("language");
  if (!Language)
    return std::nullopt;

  return StringSwitch<clang::Language>(*Language)
      .Case("c", clang::Language::C)
      .Case("c++", clang::Language::CXX)
      .Case("objective-c", clang::Language::ObjC)
      .Case("objective-c++", clang::Language::ObjCXX)
      .Default(clang::Language::Unknown);
}

Error Implementation::parseHeaders(Array &Headers) {
  for (const auto &H : Headers) {
    auto *Obj = H.getAsObject();
    if (!Obj)
      return make_error<StringError>("expect a JSON object",
                                     inconvertibleErrorCode());
    auto Type = parseType(Obj);
    if (!Type)
      return Type.takeError();
    auto Path = parsePath(Obj);
    if (!Path)
      return Path.takeError();
    auto Language = parseLanguage(Obj);

    StringRef PathStr = *Path;
    if (*Type == HeaderType::Project) {
      HeaderList.emplace_back(
          HeaderFile{PathStr, *Type, /*IncludeName=*/"", Language});
      continue;
    }

    if (FM)
      if (!FM->getOptionalFileRef(PathStr))
        return createFileError(
            PathStr, make_error_code(std::errc::no_such_file_or_directory));

    auto IncludeName = createIncludeHeaderName(PathStr);
    HeaderList.emplace_back(PathStr, *Type,
                            IncludeName.has_value() ? IncludeName.value() : "",
                            Language);
  }

  return Error::success();
}

Error Implementation::parse(StringRef Input) {
  auto Val = json::parse(Input);
  if (!Val)
    return Val.takeError();

  auto *Root = Val->getAsObject();
  if (!Root)
    return make_error<StringError>("not a JSON object",
                                   inconvertibleErrorCode());

  auto VersionStr = Root->getString("version");
  if (!VersionStr)
    return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat,
                                    "required field 'version' not specified");
  if (VersionStr->getAsInteger(10, Version))
    return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat,
                                    "invalid version number");

  if (Version < 1 || Version > 3)
    return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat,
                                    "unsupported version");

  // Not specifying any header files should be atypical, but valid.
  auto Headers = Root->getArray("headers");
  if (!Headers)
    return Error::success();

  Error Err = parseHeaders(*Headers);
  if (Err)
    return Err;

  return Error::success();
}
} // namespace

llvm::Error
FileListReader::loadHeaders(std::unique_ptr<MemoryBuffer> InputBuffer,
                            HeaderSeq &Destination, clang::FileManager *FM) {
  Implementation Impl;
  Impl.InputBuffer = std::move(InputBuffer);
  Impl.FM = FM;

  if (llvm::Error Err = Impl.parse(Impl.InputBuffer->getBuffer()))
    return Err;

  Destination.reserve(Destination.size() + Impl.HeaderList.size());
  llvm::move(Impl.HeaderList, std::back_inserter(Destination));

  return Error::success();
}