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-eagerly-assume -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 -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-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/tools/clang/lib/Sema -I /build/llvm-toolchain-snapshot-7~svn338205/tools/clang/lib/Sema -I /build/llvm-toolchain-snapshot-7~svn338205/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn338205/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/lib/gcc/x86_64-linux-gnu/8/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-class-memaccess -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/tools/clang/lib/Sema -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -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-07-29-043837-17923-1 -x c++ /build/llvm-toolchain-snapshot-7~svn338205/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 sizeof(ParsedAttr) + sizeof(void *);
45 return (sizeof(ParsedAttr) + NumArgs * sizeof(ArgsUnion));
46}
47
48AttributeFactory::AttributeFactory() {
49 // Go ahead and configure all the inline capacity. This is just a memset.
50 FreeLists.resize(InlineFreeListsCapacity);
51}
52AttributeFactory::~AttributeFactory() = default;
53
54static size_t getFreeListIndexForSize(size_t size) {
55 assert(size >= sizeof(ParsedAttr))(static_cast <bool> (size >= sizeof(ParsedAttr)) ? void
(0) : __assert_fail ("size >= sizeof(ParsedAttr)", "/build/llvm-toolchain-snapshot-7~svn338205/tools/clang/lib/Sema/ParsedAttr.cpp"
, 55, __extension__ __PRETTY_FUNCTION__))
;
56 assert((size % sizeof(void*)) == 0)(static_cast <bool> ((size % sizeof(void*)) == 0) ? void
(0) : __assert_fail ("(size % sizeof(void*)) == 0", "/build/llvm-toolchain-snapshot-7~svn338205/tools/clang/lib/Sema/ParsedAttr.cpp"
, 56, __extension__ __PRETTY_FUNCTION__))
;
57 return ((size - sizeof(ParsedAttr)) / sizeof(void *));
58}
59
60void *AttributeFactory::allocate(size_t size) {
61 // Check for a previously reclaimed attribute.
62 size_t index = getFreeListIndexForSize(size);
63 if (index < FreeLists.size() && !FreeLists[index].empty()) {
64 ParsedAttr *attr = FreeLists[index].back();
65 FreeLists[index].pop_back();
66 return attr;
67 }
68
69 // Otherwise, allocate something new.
70 return Alloc.Allocate(size, alignof(AttributeFactory));
71}
72
73void AttributeFactory::deallocate(ParsedAttr *Attr) {
74 size_t size = Attr->allocated_size();
75 size_t freeListIndex = getFreeListIndexForSize(size);
76
77 // Expand FreeLists to the appropriate size, if required.
78 if (freeListIndex >= FreeLists.size())
79 FreeLists.resize(freeListIndex + 1);
80
81#if !NDEBUG
82 // In debug mode, zero out the attribute to help find memory overwriting.
83 memset(Attr, 0, size);
84#endif
85
86 // Add 'Attr' to the appropriate free-list.
87 FreeLists[freeListIndex].push_back(Attr);
88}
89
90void AttributeFactory::reclaimPool(AttributePool &cur) {
91 for (ParsedAttr *AL : cur.Attrs)
92 deallocate(AL);
93}
94
95void AttributePool::takePool(AttributePool &pool) {
96 Attrs.insert(Attrs.end(), pool.Attrs.begin(), pool.Attrs.end());
97 pool.Attrs.clear();
98}
99
100#include "clang/Sema/AttrParsedAttrKinds.inc"
101
102static StringRef normalizeAttrName(StringRef AttrName, StringRef ScopeName,
103 ParsedAttr::Syntax SyntaxUsed) {
104 // Normalize the attribute name, __foo__ becomes foo. This is only allowable
105 // for GNU attributes.
106 bool IsGNU = SyntaxUsed == ParsedAttr::AS_GNU ||
107 ((SyntaxUsed == ParsedAttr::AS_CXX11 ||
108 SyntaxUsed == ParsedAttr::AS_C2x) &&
109 ScopeName == "gnu");
110 if (IsGNU && AttrName.size() >= 4 && AttrName.startswith("__") &&
111 AttrName.endswith("__"))
112 AttrName = AttrName.slice(2, AttrName.size() - 2);
113
114 return AttrName;
115}
116
117ParsedAttr::Kind ParsedAttr::getKind(const IdentifierInfo *Name,
118 const IdentifierInfo *ScopeName,
119 Syntax SyntaxUsed) {
120 StringRef AttrName = Name->getName();
121
122 SmallString<64> FullName;
123 if (ScopeName)
124 FullName += ScopeName->getName();
125
126 AttrName = normalizeAttrName(AttrName, FullName, SyntaxUsed);
127
128 // Ensure that in the case of C++11 attributes, we look for '::foo' if it is
129 // unscoped.
130 if (ScopeName || SyntaxUsed == AS_CXX11 || SyntaxUsed == AS_C2x)
131 FullName += "::";
132 FullName += AttrName;
133
134 return ::getAttrKind(FullName, SyntaxUsed);
135}
136
137unsigned ParsedAttr::getAttributeSpellingListIndex() const {
138 // Both variables will be used in tablegen generated
139 // attribute spell list index matching code.
140 StringRef Scope = ScopeName ? ScopeName->getName() : "";
141 StringRef Name = normalizeAttrName(AttrName->getName(), Scope,
142 (ParsedAttr::Syntax)SyntaxUsed);
143
144#include "clang/Sema/AttrSpellingListIndex.inc"
145
146}
147
148struct ParsedAttrInfo {
149 unsigned NumArgs : 4;
150 unsigned OptArgs : 4;
151 unsigned HasCustomParsing : 1;
152 unsigned IsTargetSpecific : 1;
153 unsigned IsType : 1;
154 unsigned IsStmt : 1;
155 unsigned IsKnownToGCC : 1;
156 unsigned IsSupportedByPragmaAttribute : 1;
157
158 bool (*DiagAppertainsToDecl)(Sema &S, const ParsedAttr &Attr, const Decl *);
159 bool (*DiagLangOpts)(Sema &S, const ParsedAttr &Attr);
160 bool (*ExistsInTarget)(const TargetInfo &Target);
161 unsigned (*SpellingIndexToSemanticSpelling)(const ParsedAttr &Attr);
162 void (*GetPragmaAttributeMatchRules)(
163 llvm::SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &Rules,
164 const LangOptions &LangOpts);
165};
166
167namespace {
168
169#include "clang/Sema/AttrParsedAttrImpl.inc"
170
171} // namespace
172
173static const ParsedAttrInfo &getInfo(const ParsedAttr &A) {
174 return AttrInfoMap[A.getKind()];
175}
176
177unsigned ParsedAttr::getMinArgs() const { return getInfo(*this).NumArgs; }
178
179unsigned ParsedAttr::getMaxArgs() const {
180 return getMinArgs() + getInfo(*this).OptArgs;
181}
182
183bool ParsedAttr::hasCustomParsing() const {
184 return getInfo(*this).HasCustomParsing;
185}
186
187bool ParsedAttr::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
188 return getInfo(*this).DiagAppertainsToDecl(S, *this, D);
189}
190
191bool ParsedAttr::appliesToDecl(const Decl *D,
192 attr::SubjectMatchRule MatchRule) const {
193 return checkAttributeMatchRuleAppliesTo(D, MatchRule);
194}
195
196void ParsedAttr::getMatchRules(
197 const LangOptions &LangOpts,
198 SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &MatchRules)
199 const {
200 return getInfo(*this).GetPragmaAttributeMatchRules(MatchRules, LangOpts);
201}
202
203bool ParsedAttr::diagnoseLangOpts(Sema &S) const {
204 return getInfo(*this).DiagLangOpts(S, *this);
205}
206
207bool ParsedAttr::isTargetSpecificAttr() const {
208 return getInfo(*this).IsTargetSpecific;
209}
210
211bool ParsedAttr::isTypeAttr() const { return getInfo(*this).IsType; }
212
213bool ParsedAttr::isStmtAttr() const { return getInfo(*this).IsStmt; }
214
215bool ParsedAttr::existsInTarget(const TargetInfo &Target) const {
216 return getInfo(*this).ExistsInTarget(Target);
217}
218
219bool ParsedAttr::isKnownToGCC() const { return getInfo(*this).IsKnownToGCC; }
220
221bool ParsedAttr::isSupportedByPragmaAttribute() const {
222 return getInfo(*this).IsSupportedByPragmaAttribute;
223}
224
225unsigned ParsedAttr::getSemanticSpelling() const {
226 return getInfo(*this).SpellingIndexToSemanticSpelling(*this);
227}
228
229bool ParsedAttr::hasVariadicArg() const {
230 // If the attribute has the maximum number of optional arguments, we will
231 // claim that as being variadic. If we someday get an attribute that
232 // legitimately bumps up against that maximum, we can use another bit to track
233 // whether it's truly variadic or not.
234 return getInfo(*this).OptArgs == 15;
235}