Bug Summary

File:tools/clang/lib/Sema/ParsedAttr.cpp
Warning:line 32, column 3
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ParsedAttr.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-8/lib/clang/8.0.0 -D CLANG_VENDOR="Debian " -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-8~svn350071/build-llvm/tools/clang/lib/Sema -I /build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/Sema -I /build/llvm-toolchain-snapshot-8~svn350071/tools/clang/include -I /build/llvm-toolchain-snapshot-8~svn350071/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-8~svn350071/build-llvm/include -I /build/llvm-toolchain-snapshot-8~svn350071/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/8.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-8/lib/clang/8.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-8~svn350071/build-llvm/tools/clang/lib/Sema -fdebug-prefix-map=/build/llvm-toolchain-snapshot-8~svn350071=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-12-27-042839-1215-1 -x c++ /build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/Sema/ParsedAttr.cpp -faddrsig
1//======- ParsedAttr.cpp --------------------------------------------------===//
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// This file defines the ParsedAttr class implementation
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Sema/ParsedAttr.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/Basic/AttrSubjectMatchRules.h"
17#include "clang/Basic/IdentifierTable.h"
18#include "clang/Basic/TargetInfo.h"
19#include "clang/Sema/SemaInternal.h"
20#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include <cassert>
24#include <cstddef>
25#include <utility>
26
27using namespace clang;
28
29IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc,
30 IdentifierInfo *Ident) {
31 IdentifierLoc *Result = new (Ctx) IdentifierLoc;
1
'Result' initialized to a null pointer value
32 Result->Loc = Loc;
2
Called C++ object pointer is null
33 Result->Ident = Ident;
34 return Result;
35}
36
37size_t ParsedAttr::allocated_size() const {
38 if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
39 else if (IsTypeTagForDatatype)
40 return AttributeFactory::TypeTagForDatatypeAllocSize;
41 else if (IsProperty)
42 return AttributeFactory::PropertyAllocSize;
43 else if (HasParsedType)
44 return totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
45 detail::TypeTagForDatatypeData, ParsedType,
46 detail::PropertyData>(0, 0, 0, 1, 0);
47 return totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
48 detail::TypeTagForDatatypeData, ParsedType,
49 detail::PropertyData>(NumArgs, 0, 0, 0, 0);
50}
51
52AttributeFactory::AttributeFactory() {
53 // Go ahead and configure all the inline capacity. This is just a memset.
54 FreeLists.resize(InlineFreeListsCapacity);
55}
56AttributeFactory::~AttributeFactory() = default;
57
58static size_t getFreeListIndexForSize(size_t size) {
59 assert(size >= sizeof(ParsedAttr))((size >= sizeof(ParsedAttr)) ? static_cast<void> (0
) : __assert_fail ("size >= sizeof(ParsedAttr)", "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/Sema/ParsedAttr.cpp"
, 59, __PRETTY_FUNCTION__))
;
60 assert((size % sizeof(void*)) == 0)(((size % sizeof(void*)) == 0) ? static_cast<void> (0) :
__assert_fail ("(size % sizeof(void*)) == 0", "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/Sema/ParsedAttr.cpp"
, 60, __PRETTY_FUNCTION__))
;
61 return ((size - sizeof(ParsedAttr)) / sizeof(void *));
62}
63
64void *AttributeFactory::allocate(size_t size) {
65 // Check for a previously reclaimed attribute.
66 size_t index = getFreeListIndexForSize(size);
67 if (index < FreeLists.size() && !FreeLists[index].empty()) {
68 ParsedAttr *attr = FreeLists[index].back();
69 FreeLists[index].pop_back();
70 return attr;
71 }
72
73 // Otherwise, allocate something new.
74 return Alloc.Allocate(size, alignof(AttributeFactory));
75}
76
77void AttributeFactory::deallocate(ParsedAttr *Attr) {
78 size_t size = Attr->allocated_size();
79 size_t freeListIndex = getFreeListIndexForSize(size);
80
81 // Expand FreeLists to the appropriate size, if required.
82 if (freeListIndex >= FreeLists.size())
83 FreeLists.resize(freeListIndex + 1);
84
85#ifndef NDEBUG
86 // In debug mode, zero out the attribute to help find memory overwriting.
87 memset(Attr, 0, size);
88#endif
89
90 // Add 'Attr' to the appropriate free-list.
91 FreeLists[freeListIndex].push_back(Attr);
92}
93
94void AttributeFactory::reclaimPool(AttributePool &cur) {
95 for (ParsedAttr *AL : cur.Attrs)
96 deallocate(AL);
97}
98
99void AttributePool::takePool(AttributePool &pool) {
100 Attrs.insert(Attrs.end(), pool.Attrs.begin(), pool.Attrs.end());
101 pool.Attrs.clear();
102}
103
104#include "clang/Sema/AttrParsedAttrKinds.inc"
105
106static StringRef normalizeAttrScopeName(StringRef ScopeName,
107 ParsedAttr::Syntax SyntaxUsed) {
108 // Normalize the "__gnu__" scope name to be "gnu" and the "_Clang" scope name
109 // to be "clang".
110 if (SyntaxUsed == ParsedAttr::AS_CXX11 ||
111 SyntaxUsed == ParsedAttr::AS_C2x) {
112 if (ScopeName == "__gnu__")
113 ScopeName = "gnu";
114 else if (ScopeName == "_Clang")
115 ScopeName = "clang";
116 }
117 return ScopeName;
118}
119
120static StringRef normalizeAttrName(StringRef AttrName,
121 StringRef NormalizedScopeName,
122 ParsedAttr::Syntax SyntaxUsed) {
123 // Normalize the attribute name, __foo__ becomes foo. This is only allowable
124 // for GNU attributes, and attributes using the double square bracket syntax.
125 bool ShouldNormalize =
126 SyntaxUsed == ParsedAttr::AS_GNU ||
127 ((SyntaxUsed == ParsedAttr::AS_CXX11 ||
128 SyntaxUsed == ParsedAttr::AS_C2x) &&
129 (NormalizedScopeName == "gnu" || NormalizedScopeName == "clang"));
130 if (ShouldNormalize && AttrName.size() >= 4 && AttrName.startswith("__") &&
131 AttrName.endswith("__"))
132 AttrName = AttrName.slice(2, AttrName.size() - 2);
133
134 return AttrName;
135}
136
137ParsedAttr::Kind ParsedAttr::getKind(const IdentifierInfo *Name,
138 const IdentifierInfo *ScopeName,
139 Syntax SyntaxUsed) {
140 StringRef AttrName = Name->getName();
141
142 SmallString<64> FullName;
143 if (ScopeName)
144 FullName += normalizeAttrScopeName(ScopeName->getName(), SyntaxUsed);
145
146 AttrName = normalizeAttrName(AttrName, FullName, SyntaxUsed);
147
148 // Ensure that in the case of C++11 attributes, we look for '::foo' if it is
149 // unscoped.
150 if (ScopeName || SyntaxUsed == AS_CXX11 || SyntaxUsed == AS_C2x)
151 FullName += "::";
152 FullName += AttrName;
153
154 return ::getAttrKind(FullName, SyntaxUsed);
155}
156
157unsigned ParsedAttr::getAttributeSpellingListIndex() const {
158 // Both variables will be used in tablegen generated
159 // attribute spell list index matching code.
160 auto Syntax = static_cast<ParsedAttr::Syntax>(SyntaxUsed);
161 StringRef Scope =
162 ScopeName ? normalizeAttrScopeName(ScopeName->getName(), Syntax) : "";
163 StringRef Name = normalizeAttrName(AttrName->getName(), Scope, Syntax);
164
165#include "clang/Sema/AttrSpellingListIndex.inc"
166
167}
168
169struct ParsedAttrInfo {
170 unsigned NumArgs : 4;
171 unsigned OptArgs : 4;
172 unsigned HasCustomParsing : 1;
173 unsigned IsTargetSpecific : 1;
174 unsigned IsType : 1;
175 unsigned IsStmt : 1;
176 unsigned IsKnownToGCC : 1;
177 unsigned IsSupportedByPragmaAttribute : 1;
178
179 bool (*DiagAppertainsToDecl)(Sema &S, const ParsedAttr &Attr, const Decl *);
180 bool (*DiagLangOpts)(Sema &S, const ParsedAttr &Attr);
181 bool (*ExistsInTarget)(const TargetInfo &Target);
182 unsigned (*SpellingIndexToSemanticSpelling)(const ParsedAttr &Attr);
183 void (*GetPragmaAttributeMatchRules)(
184 llvm::SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &Rules,
185 const LangOptions &LangOpts);
186};
187
188namespace {
189
190#include "clang/Sema/AttrParsedAttrImpl.inc"
191
192} // namespace
193
194static const ParsedAttrInfo &getInfo(const ParsedAttr &A) {
195 return AttrInfoMap[A.getKind()];
196}
197
198unsigned ParsedAttr::getMinArgs() const { return getInfo(*this).NumArgs; }
199
200unsigned ParsedAttr::getMaxArgs() const {
201 return getMinArgs() + getInfo(*this).OptArgs;
202}
203
204bool ParsedAttr::hasCustomParsing() const {
205 return getInfo(*this).HasCustomParsing;
206}
207
208bool ParsedAttr::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
209 return getInfo(*this).DiagAppertainsToDecl(S, *this, D);
210}
211
212bool ParsedAttr::appliesToDecl(const Decl *D,
213 attr::SubjectMatchRule MatchRule) const {
214 return checkAttributeMatchRuleAppliesTo(D, MatchRule);
215}
216
217void ParsedAttr::getMatchRules(
218 const LangOptions &LangOpts,
219 SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &MatchRules)
220 const {
221 return getInfo(*this).GetPragmaAttributeMatchRules(MatchRules, LangOpts);
222}
223
224bool ParsedAttr::diagnoseLangOpts(Sema &S) const {
225 return getInfo(*this).DiagLangOpts(S, *this);
226}
227
228bool ParsedAttr::isTargetSpecificAttr() const {
229 return getInfo(*this).IsTargetSpecific;
230}
231
232bool ParsedAttr::isTypeAttr() const { return getInfo(*this).IsType; }
233
234bool ParsedAttr::isStmtAttr() const { return getInfo(*this).IsStmt; }
235
236bool ParsedAttr::existsInTarget(const TargetInfo &Target) const {
237 return getInfo(*this).ExistsInTarget(Target);
238}
239
240bool ParsedAttr::isKnownToGCC() const { return getInfo(*this).IsKnownToGCC; }
241
242bool ParsedAttr::isSupportedByPragmaAttribute() const {
243 return getInfo(*this).IsSupportedByPragmaAttribute;
244}
245
246unsigned ParsedAttr::getSemanticSpelling() const {
247 return getInfo(*this).SpellingIndexToSemanticSpelling(*this);
248}
249
250bool ParsedAttr::hasVariadicArg() const {
251 // If the attribute has the maximum number of optional arguments, we will
252 // claim that as being variadic. If we someday get an attribute that
253 // legitimately bumps up against that maximum, we can use another bit to track
254 // whether it's truly variadic or not.
255 return getInfo(*this).OptArgs == 15;
256}