Bug Summary

File:tools/clang/lib/Format/AffectedRangeManager.cpp
Warning:line 138, column 7
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 AffectedRangeManager.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 -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 _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/clang/lib/Format -I /build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/Format -I /build/llvm-toolchain-snapshot-8~svn345461/tools/clang/include -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/include -I /build/llvm-toolchain-snapshot-8~svn345461/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~svn345461/build-llvm/tools/clang/lib/Format -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-10-27-211344-32123-1 -x c++ /build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/Format/AffectedRangeManager.cpp -faddrsig
1//===--- AffectedRangeManager.cpp - Format C++ code -----------------------===//
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/// \file
11/// This file implements AffectRangeManager class.
12///
13//===----------------------------------------------------------------------===//
14
15#include "AffectedRangeManager.h"
16
17#include "FormatToken.h"
18#include "TokenAnnotator.h"
19
20namespace clang {
21namespace format {
22
23bool AffectedRangeManager::computeAffectedLines(
24 SmallVectorImpl<AnnotatedLine *> &Lines) {
25 SmallVectorImpl<AnnotatedLine *>::iterator I = Lines.begin();
26 SmallVectorImpl<AnnotatedLine *>::iterator E = Lines.end();
27 bool SomeLineAffected = false;
28 const AnnotatedLine *PreviousLine = nullptr;
29 while (I != E) {
1
Assuming 'I' is not equal to 'E'
2
Loop condition is true. Entering loop body
30 AnnotatedLine *Line = *I;
31 Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
32
33 // If a line is part of a preprocessor directive, it needs to be formatted
34 // if any token within the directive is affected.
35 if (Line->InPPDirective) {
3
Assuming the condition is false
4
Taking false branch
36 FormatToken *Last = Line->Last;
37 SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1;
38 while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) {
39 Last = (*PPEnd)->Last;
40 ++PPEnd;
41 }
42
43 if (affectsTokenRange(*Line->First, *Last,
44 /*IncludeLeadingNewlines=*/false)) {
45 SomeLineAffected = true;
46 markAllAsAffected(I, PPEnd);
47 }
48 I = PPEnd;
49 continue;
50 }
51
52 if (nonPPLineAffected(Line, PreviousLine, Lines))
5
Calling 'AffectedRangeManager::nonPPLineAffected'
53 SomeLineAffected = true;
54
55 PreviousLine = Line;
56 ++I;
57 }
58 return SomeLineAffected;
59}
60
61bool AffectedRangeManager::affectsCharSourceRange(
62 const CharSourceRange &Range) {
63 for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
64 E = Ranges.end();
65 I != E; ++I) {
66 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) &&
67 !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin()))
68 return true;
69 }
70 return false;
71}
72
73bool AffectedRangeManager::affectsTokenRange(const FormatToken &First,
74 const FormatToken &Last,
75 bool IncludeLeadingNewlines) {
76 SourceLocation Start = First.WhitespaceRange.getBegin();
77 if (!IncludeLeadingNewlines)
78 Start = Start.getLocWithOffset(First.LastNewlineOffset);
79 SourceLocation End = Last.getStartOfNonWhitespace();
80 End = End.getLocWithOffset(Last.TokenText.size());
81 CharSourceRange Range = CharSourceRange::getCharRange(Start, End);
82 return affectsCharSourceRange(Range);
83}
84
85bool AffectedRangeManager::affectsLeadingEmptyLines(const FormatToken &Tok) {
86 CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
87 Tok.WhitespaceRange.getBegin(),
88 Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
89 return affectsCharSourceRange(EmptyLineRange);
90}
91
92void AffectedRangeManager::markAllAsAffected(
93 SmallVectorImpl<AnnotatedLine *>::iterator I,
94 SmallVectorImpl<AnnotatedLine *>::iterator E) {
95 while (I != E) {
96 (*I)->Affected = true;
97 markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
98 ++I;
99 }
100}
101
102bool AffectedRangeManager::nonPPLineAffected(
103 AnnotatedLine *Line, const AnnotatedLine *PreviousLine,
104 SmallVectorImpl<AnnotatedLine *> &Lines) {
105 bool SomeLineAffected = false;
106 Line->ChildrenAffected = computeAffectedLines(Line->Children);
6
Value assigned to field 'First'
107 if (Line->ChildrenAffected)
7
Assuming the condition is false
8
Taking false branch
108 SomeLineAffected = true;
109
110 // Stores whether one of the line's tokens is directly affected.
111 bool SomeTokenAffected = false;
112 // Stores whether we need to look at the leading newlines of the next token
113 // in order to determine whether it was affected.
114 bool IncludeLeadingNewlines = false;
115
116 // Stores whether the first child line of any of this line's tokens is
117 // affected.
118 bool SomeFirstChildAffected = false;
119
120 for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
9
Assuming pointer value is null
10
Loop condition is false. Execution continues on line 134
121 // Determine whether 'Tok' was affected.
122 if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
123 SomeTokenAffected = true;
124
125 // Determine whether the first child of 'Tok' was affected.
126 if (!Tok->Children.empty() && Tok->Children.front()->Affected)
127 SomeFirstChildAffected = true;
128
129 IncludeLeadingNewlines = Tok->Children.empty();
130 }
131
132 // Was this line moved, i.e. has it previously been on the same line as an
133 // affected line?
134 bool LineMoved = PreviousLine && PreviousLine->Affected &&
135 Line->First->NewlinesBefore == 0;
136
137 bool IsContinuedComment =
138 Line->First->is(tok::comment) && Line->First->Next == nullptr &&
11
Called C++ object pointer is null
139 Line->First->NewlinesBefore < 2 && PreviousLine &&
140 PreviousLine->Affected && PreviousLine->Last->is(tok::comment);
141
142 bool IsAffectedClosingBrace =
143 Line->First->is(tok::r_brace) &&
144 Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
145 Lines[Line->MatchingOpeningBlockLineIndex]->Affected;
146
147 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
148 IsContinuedComment || IsAffectedClosingBrace) {
149 Line->Affected = true;
150 SomeLineAffected = true;
151 }
152 return SomeLineAffected;
153}
154
155} // namespace format
156} // namespace clang