xref: /freebsd-src/contrib/llvm-project/clang/lib/ExtractAPI/APIIgnoresList.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1bdd1243dSDimitry Andric //===- ExtractAPI/APIIgnoresList.cpp -------*- C++ -*-===//
2bdd1243dSDimitry Andric //
3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bdd1243dSDimitry Andric //
7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
8bdd1243dSDimitry Andric ///
9bdd1243dSDimitry Andric /// \file
10bdd1243dSDimitry Andric /// This file implements APIIgnoresList that allows users to specifiy a file
11bdd1243dSDimitry Andric /// containing symbols to ignore during API extraction.
12bdd1243dSDimitry Andric ///
13bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
14bdd1243dSDimitry Andric 
15bdd1243dSDimitry Andric #include "clang/ExtractAPI/APIIgnoresList.h"
16bdd1243dSDimitry Andric #include "clang/Basic/FileManager.h"
17bdd1243dSDimitry Andric #include "llvm/ADT/STLExtras.h"
18bdd1243dSDimitry Andric #include "llvm/Support/Error.h"
19bdd1243dSDimitry Andric 
20bdd1243dSDimitry Andric using namespace clang;
21bdd1243dSDimitry Andric using namespace clang::extractapi;
22bdd1243dSDimitry Andric using namespace llvm;
23bdd1243dSDimitry Andric 
24bdd1243dSDimitry Andric char IgnoresFileNotFound::ID;
25bdd1243dSDimitry Andric 
log(llvm::raw_ostream & os) const26bdd1243dSDimitry Andric void IgnoresFileNotFound::log(llvm::raw_ostream &os) const {
27bdd1243dSDimitry Andric   os << "Could not find API ignores file " << Path;
28bdd1243dSDimitry Andric }
29bdd1243dSDimitry Andric 
convertToErrorCode() const30bdd1243dSDimitry Andric std::error_code IgnoresFileNotFound::convertToErrorCode() const {
31bdd1243dSDimitry Andric   return llvm::inconvertibleErrorCode();
32bdd1243dSDimitry Andric }
33bdd1243dSDimitry Andric 
34*06c3fb27SDimitry Andric Expected<APIIgnoresList>
create(const FilePathList & IgnoresFilePathList,FileManager & FM)35*06c3fb27SDimitry Andric APIIgnoresList::create(const FilePathList &IgnoresFilePathList,
36bdd1243dSDimitry Andric                        FileManager &FM) {
37*06c3fb27SDimitry Andric   SmallVector<StringRef, 32> Lines;
38*06c3fb27SDimitry Andric   BufferList symbolBufferList;
39*06c3fb27SDimitry Andric 
40*06c3fb27SDimitry Andric   for (const auto &CurrentIgnoresFilePath : IgnoresFilePathList) {
41*06c3fb27SDimitry Andric     auto BufferOrErr = FM.getBufferForFile(CurrentIgnoresFilePath);
42*06c3fb27SDimitry Andric 
43bdd1243dSDimitry Andric     if (!BufferOrErr)
44*06c3fb27SDimitry Andric       return make_error<IgnoresFileNotFound>(CurrentIgnoresFilePath);
45bdd1243dSDimitry Andric 
46bdd1243dSDimitry Andric     auto Buffer = std::move(BufferOrErr.get());
47*06c3fb27SDimitry Andric     Buffer->getBuffer().split(Lines, '\n', /*MaxSplit*/ -1,
48*06c3fb27SDimitry Andric                               /*KeepEmpty*/ false);
49*06c3fb27SDimitry Andric     symbolBufferList.push_back(std::move(Buffer));
50*06c3fb27SDimitry Andric   }
51*06c3fb27SDimitry Andric 
52*06c3fb27SDimitry Andric   // Symbol names don't have spaces in them, let's just remove these in case
53*06c3fb27SDimitry Andric   // the input is slighlty malformed.
54bdd1243dSDimitry Andric   transform(Lines, Lines.begin(), [](StringRef Line) { return Line.trim(); });
55bdd1243dSDimitry Andric   sort(Lines);
56*06c3fb27SDimitry Andric   return APIIgnoresList(std::move(Lines), std::move(symbolBufferList));
57bdd1243dSDimitry Andric }
58bdd1243dSDimitry Andric 
shouldIgnore(StringRef SymbolName) const59bdd1243dSDimitry Andric bool APIIgnoresList::shouldIgnore(StringRef SymbolName) const {
60bdd1243dSDimitry Andric   auto It = lower_bound(SymbolsToIgnore, SymbolName);
61bdd1243dSDimitry Andric   return (It != SymbolsToIgnore.end()) && (*It == SymbolName);
62bdd1243dSDimitry Andric }
63