clang  14.0.0git
SelectorLocationsKind.cpp
Go to the documentation of this file.
1 //===--- SelectorLocationsKind.cpp - Kind of selector locations -*- 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 // Describes whether the identifier locations for a selector are "standard"
10 // or not.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "clang/AST/Expr.h"
16 
17 using namespace clang;
18 
19 static SourceLocation getStandardSelLoc(unsigned Index,
20  Selector Sel,
21  bool WithArgSpace,
22  SourceLocation ArgLoc,
23  SourceLocation EndLoc) {
24  unsigned NumSelArgs = Sel.getNumArgs();
25  if (NumSelArgs == 0) {
26  assert(Index == 0);
27  if (EndLoc.isInvalid())
28  return SourceLocation();
30  unsigned Len = II ? II->getLength() : 0;
31  return EndLoc.getLocWithOffset(-Len);
32  }
33 
34  assert(Index < NumSelArgs);
35  if (ArgLoc.isInvalid())
36  return SourceLocation();
38  unsigned Len = /* selector id */ (II ? II->getLength() : 0) + /* ':' */ 1;
39  if (WithArgSpace)
40  ++Len;
41  return ArgLoc.getLocWithOffset(-Len);
42 }
43 
44 namespace {
45 
46 template <typename T>
47 SourceLocation getArgLoc(T* Arg);
48 
49 template <>
50 SourceLocation getArgLoc<Expr>(Expr *Arg) {
51  return Arg->getBeginLoc();
52 }
53 
54 template <>
55 SourceLocation getArgLoc<ParmVarDecl>(ParmVarDecl *Arg) {
56  SourceLocation Loc = Arg->getBeginLoc();
57  if (Loc.isInvalid())
58  return Loc;
59  // -1 to point to left paren of the method parameter's type.
60  return Loc.getLocWithOffset(-1);
61 }
62 
63 template <typename T>
64 SourceLocation getArgLoc(unsigned Index, ArrayRef<T*> Args) {
65  return Index < Args.size() ? getArgLoc(Args[Index]) : SourceLocation();
66 }
67 
68 template <typename T>
69 SelectorLocationsKind hasStandardSelLocs(Selector Sel,
71  ArrayRef<T *> Args,
72  SourceLocation EndLoc) {
73  // Are selector locations in standard position with no space between args ?
74  unsigned i;
75  for (i = 0; i != SelLocs.size(); ++i) {
76  if (SelLocs[i] != getStandardSelectorLoc(i, Sel, /*WithArgSpace=*/false,
77  Args, EndLoc))
78  break;
79  }
80  if (i == SelLocs.size())
82 
83  // Are selector locations in standard position with space between args ?
84  for (i = 0; i != SelLocs.size(); ++i) {
85  if (SelLocs[i] != getStandardSelectorLoc(i, Sel, /*WithArgSpace=*/true,
86  Args, EndLoc))
87  return SelLoc_NonStandard;
88  }
89 
91 }
92 
93 } // anonymous namespace
94 
98  ArrayRef<Expr *> Args,
99  SourceLocation EndLoc) {
100  return hasStandardSelLocs(Sel, SelLocs, Args, EndLoc);
101 }
102 
104  Selector Sel,
105  bool WithArgSpace,
106  ArrayRef<Expr *> Args,
107  SourceLocation EndLoc) {
108  return getStandardSelLoc(Index, Sel, WithArgSpace,
109  getArgLoc(Index, Args), EndLoc);
110 }
111 
114  ArrayRef<SourceLocation> SelLocs,
116  SourceLocation EndLoc) {
117  return hasStandardSelLocs(Sel, SelLocs, Args, EndLoc);
118 }
119 
121  Selector Sel,
122  bool WithArgSpace,
124  SourceLocation EndLoc) {
125  return getStandardSelLoc(Index, Sel, WithArgSpace,
126  getArgLoc(Index, Args), EndLoc);
127 }
clang::getStandardSelectorLoc
SourceLocation getStandardSelectorLoc(unsigned Index, Selector Sel, bool WithArgSpace, ArrayRef< Expr * > Args, SourceLocation EndLoc)
Get the "standard" location of a selector identifier, e.g: For nullary selectors, immediately before ...
Definition: SelectorLocationsKind.cpp:103
clang::DeclaratorDecl::getBeginLoc
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Decl.h:780
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::SourceLocation::getLocWithOffset
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
Definition: SourceLocation.h:136
clang::SelLoc_NonStandard
@ SelLoc_NonStandard
Non-standard.
Definition: SelectorLocationsKind.h:29
clang::ParmVarDecl
Represents a parameter to a function.
Definition: Decl.h:1665
clang::SelLoc_StandardWithSpace
@ SelLoc_StandardWithSpace
For nullary selectors, immediately before the end: "[foo release]" / "-(void)release;" Or with a spac...
Definition: SelectorLocationsKind.h:41
clang::SelLoc_StandardNoSpace
@ SelLoc_StandardNoSpace
For nullary selectors, immediately before the end: "[foo release]" / "-(void)release;" Or immediately...
Definition: SelectorLocationsKind.h:35
clang::Selector::getNumArgs
unsigned getNumArgs() const
Definition: IdentifierTable.cpp:480
clang::IdentifierInfo::getLength
unsigned getLength() const
Efficiently return the length of this identifier info.
Definition: IdentifierTable.h:192
Expr.h
clang::SelectorLocationsKind
SelectorLocationsKind
Whether all locations of the selector identifiers are in a "standard" position.
Definition: SelectorLocationsKind.h:27
getStandardSelLoc
static SourceLocation getStandardSelLoc(unsigned Index, Selector Sel, bool WithArgSpace, SourceLocation ArgLoc, SourceLocation EndLoc)
Definition: SelectorLocationsKind.cpp:19
SelectorLocationsKind.h
clang::hasStandardSelectorLocs
SelectorLocationsKind hasStandardSelectorLocs(Selector Sel, ArrayRef< SourceLocation > SelLocs, ArrayRef< Expr * > Args, SourceLocation EndLoc)
Returns true if all SelLocs are in a "standard" location.
Definition: SelectorLocationsKind.cpp:96
llvm::ArrayRef
Definition: LLVM.h:34
clang::Selector::getIdentifierInfoForSlot
IdentifierInfo * getIdentifierInfoForSlot(unsigned argIndex) const
Retrieve the identifier at a given position in the selector.
Definition: IdentifierTable.cpp:491
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:84
clang::SourceLocation::isInvalid
bool isInvalid() const
Definition: SourceLocation.h:113
clang
Definition: CalledOnceCheck.h:17
clang::Selector
Smart pointer class that efficiently represents Objective-C method names.
Definition: IdentifierTable.h:748
clang::Stmt::getBeginLoc
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:336
clang::Expr
This represents one expression.
Definition: Expr.h:109