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