xref: /freebsd/contrib/llvm-project/llvm/lib/Frontend/Directive/Spelling.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1*700637cbSDimitry Andric //===-------------------------------------------------------------- C++ -*-===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric 
9*700637cbSDimitry Andric #include "llvm/Frontend/Directive/Spelling.h"
10*700637cbSDimitry Andric 
11*700637cbSDimitry Andric #include "llvm/ADT/StringRef.h"
12*700637cbSDimitry Andric #include "llvm/Support/MathExtras.h"
13*700637cbSDimitry Andric 
14*700637cbSDimitry Andric #include <cassert>
15*700637cbSDimitry Andric 
16*700637cbSDimitry Andric using namespace llvm;
17*700637cbSDimitry Andric 
Contains(directive::VersionRange V,int P)18*700637cbSDimitry Andric static bool Contains(directive::VersionRange V, int P) {
19*700637cbSDimitry Andric   return V.Min <= P && P <= V.Max;
20*700637cbSDimitry Andric }
21*700637cbSDimitry Andric 
FindName(llvm::iterator_range<const directive::Spelling * > Range,unsigned Version)22*700637cbSDimitry Andric llvm::StringRef llvm::directive::FindName(
23*700637cbSDimitry Andric     llvm::iterator_range<const directive::Spelling *> Range, unsigned Version) {
24*700637cbSDimitry Andric   assert(llvm::isInt<8 * sizeof(int)>(Version) && "Version value out of range");
25*700637cbSDimitry Andric 
26*700637cbSDimitry Andric   int V = Version;
27*700637cbSDimitry Andric   // Do a linear search to find the first Spelling that contains Version.
28*700637cbSDimitry Andric   // The condition "contains(S, Version)" does not partition the list of
29*700637cbSDimitry Andric   // spellings, so std::[lower|upper]_bound cannot be used.
30*700637cbSDimitry Andric   // In practice the list of spellings is expected to be very short, so
31*700637cbSDimitry Andric   // linear search seems appropriate. In general, an interval tree may be
32*700637cbSDimitry Andric   // a better choice, but in this case it may be an overkill.
33*700637cbSDimitry Andric   for (auto &S : Range) {
34*700637cbSDimitry Andric     if (Contains(S.Versions, V))
35*700637cbSDimitry Andric       return S.Name;
36*700637cbSDimitry Andric   }
37*700637cbSDimitry Andric   return StringRef();
38*700637cbSDimitry Andric }
39