clang 8.0.0

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