xref: /freebsd/contrib/llvm-project/clang/lib/ExtractAPI/APIIgnoresList.cpp (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 //===- ExtractAPI/APIIgnoresList.cpp -------*- C++ -*-===//
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 /// \file
10 /// This file implements APIIgnoresList that allows users to specifiy a file
11 /// containing symbols to ignore during API extraction.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/ExtractAPI/APIIgnoresList.h"
16 #include "clang/Basic/FileManager.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Support/Error.h"
19 
20 using namespace clang;
21 using namespace clang::extractapi;
22 using namespace llvm;
23 
24 char IgnoresFileNotFound::ID;
25 
26 void IgnoresFileNotFound::log(llvm::raw_ostream &os) const {
27   os << "Could not find API ignores file " << Path;
28 }
29 
30 std::error_code IgnoresFileNotFound::convertToErrorCode() const {
31   return llvm::inconvertibleErrorCode();
32 }
33 
34 Expected<APIIgnoresList> APIIgnoresList::create(StringRef IgnoresFilePath,
35                                                 FileManager &FM) {
36   auto BufferOrErr = FM.getBufferForFile(IgnoresFilePath);
37   if (!BufferOrErr)
38     return make_error<IgnoresFileNotFound>(IgnoresFilePath);
39 
40   auto Buffer = std::move(BufferOrErr.get());
41   SmallVector<StringRef, 32> Lines;
42   Buffer->getBuffer().split(Lines, '\n', /*MaxSplit*/ -1, /*KeepEmpty*/ false);
43   // Symbol names don't have spaces in them, let's just remove these in case the
44   // input is slighlty malformed.
45   transform(Lines, Lines.begin(), [](StringRef Line) { return Line.trim(); });
46   sort(Lines);
47   return APIIgnoresList(std::move(Lines), std::move(Buffer));
48 }
49 
50 bool APIIgnoresList::shouldIgnore(StringRef SymbolName) const {
51   auto It = lower_bound(SymbolsToIgnore, SymbolName);
52   return (It != SymbolsToIgnore.end()) && (*It == SymbolName);
53 }
54