Bug Summary

File:clang/lib/Driver/ToolChains/MSP430.cpp
Warning:line 104, column 12
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 MSP430.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 -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.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-10~+201911111502510600c19528f1809/build-llvm/tools/clang/lib/Driver -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Driver -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/include -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/include -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/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/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.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++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/tools/clang/lib/Driver -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2019-12-09-002921-48462-1 -x c++ /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Driver/ToolChains/MSP430.cpp
1//===--- MSP430.cpp - MSP430 Helpers for Tools ------------------*- 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#include "MSP430.h"
10#include "CommonArgs.h"
11#include "Gnu.h"
12#include "InputInfo.h"
13#include "clang/Driver/Compilation.h"
14#include "clang/Driver/Multilib.h"
15#include "clang/Driver/Options.h"
16#include "llvm/Option/ArgList.h"
17#include "llvm/Support/FileSystem.h"
18#include "llvm/Support/Path.h"
19
20using namespace clang::driver;
21using namespace clang::driver::toolchains;
22using namespace clang::driver::tools;
23using namespace clang;
24using namespace llvm::opt;
25
26static bool isSupportedMCU(const StringRef MCU) {
27 return llvm::StringSwitch<bool>(MCU)
28#define MSP430_MCU(NAME) .Case(NAME, true)
29#include "clang/Basic/MSP430Target.def"
30 .Default(false);
31}
32
33static StringRef getSupportedHWMult(const Arg *MCU) {
34 if (!MCU)
35 return "none";
36
37 return llvm::StringSwitch<StringRef>(MCU->getValue())
38#define MSP430_MCU_FEAT(NAME, HWMULT) .Case(NAME, HWMULT)
39#include "clang/Basic/MSP430Target.def"
40 .Default("none");
41}
42
43static StringRef getHWMultLib(const ArgList &Args) {
44 StringRef HWMult = Args.getLastArgValue(options::OPT_mhwmult_EQ, "auto");
45 if (HWMult == "auto") {
46 HWMult = getSupportedHWMult(Args.getLastArg(options::OPT_mmcu_EQ));
47 }
48
49 return llvm::StringSwitch<StringRef>(HWMult)
50 .Case("16bit", "-lmul_16")
51 .Case("32bit", "-lmul_32")
52 .Case("f5series", "-lmul_f5")
53 .Default("-lmul_none");
54}
55
56void msp430::getMSP430TargetFeatures(const Driver &D, const ArgList &Args,
57 std::vector<StringRef> &Features) {
58 const Arg *MCU = Args.getLastArg(options::OPT_mmcu_EQ);
59 if (MCU && !isSupportedMCU(MCU->getValue())) {
1
Assuming 'MCU' is non-null
2
Taking false branch
60 D.Diag(diag::err_drv_clang_unsupported) << MCU->getValue();
61 return;
62 }
63
64 const Arg *HWMultArg = Args.getLastArg(options::OPT_mhwmult_EQ);
3
'HWMultArg' initialized here
65 if (!MCU
3.1
'MCU' is non-null
&& !HWMultArg)
66 return;
67
68 StringRef HWMult = HWMultArg ? HWMultArg->getValue() : "auto";
4
Assuming 'HWMultArg' is null
5
'?' condition is false
69 StringRef SupportedHWMult = getSupportedHWMult(MCU);
70
71 if (HWMult == "auto") {
6
Assuming the condition is false
7
Taking false branch
72 // 'auto' - deduce hw multiplier support based on mcu name provided.
73 // If no mcu name is provided, assume no hw multiplier is supported.
74 if (!MCU)
75 D.Diag(clang::diag::warn_drv_msp430_hwmult_no_device);
76 HWMult = SupportedHWMult;
77 }
78
79 if (HWMult == "none") {
8
Assuming the condition is false
9
Taking false branch
80 // 'none' - disable hw multiplier.
81 Features.push_back("-hwmult16");
82 Features.push_back("-hwmult32");
83 Features.push_back("-hwmultf5");
84 return;
85 }
86
87 if (MCU
9.1
'MCU' is non-null
&& SupportedHWMult == "none")
10
Assuming the condition is false
11
Taking false branch
88 D.Diag(clang::diag::warn_drv_msp430_hwmult_unsupported) << HWMult;
89 if (MCU
11.1
'MCU' is non-null
&& HWMult != SupportedHWMult)
12
Taking false branch
90 D.Diag(clang::diag::warn_drv_msp430_hwmult_mismatch)
91 << SupportedHWMult << HWMult;
92
93 if (HWMult == "16bit") {
13
Assuming the condition is false
14
Taking false branch
94 // '16bit' - for 16-bit only hw multiplier.
95 Features.push_back("+hwmult16");
96 } else if (HWMult == "32bit") {
15
Assuming the condition is false
16
Taking false branch
97 // '32bit' - for 16/32-bit hw multiplier.
98 Features.push_back("+hwmult32");
99 } else if (HWMult == "f5series") {
17
Assuming the condition is false
18
Taking false branch
100 // 'f5series' - for 16/32-bit hw multiplier supported by F5 series mcus.
101 Features.push_back("+hwmultf5");
102 } else {
103 D.Diag(clang::diag::err_drv_unsupported_option_argument)
104 << HWMultArg->getAsString(Args) << HWMult;
19
Called C++ object pointer is null
105 }
106}
107
108/// MSP430 Toolchain
109MSP430ToolChain::MSP430ToolChain(const Driver &D, const llvm::Triple &Triple,
110 const ArgList &Args)
111 : Generic_ELF(D, Triple, Args) {
112
113 StringRef MultilibSuf = "";
114
115 GCCInstallation.init(Triple, Args);
116 if (GCCInstallation.isValid()) {
117 MultilibSuf = GCCInstallation.getMultilib().gccSuffix();
118
119 SmallString<128> GCCBinPath;
120 llvm::sys::path::append(GCCBinPath,
121 GCCInstallation.getParentLibPath(), "..", "bin");
122 addPathIfExists(D, GCCBinPath, getProgramPaths());
123
124 SmallString<128> GCCRtPath;
125 llvm::sys::path::append(GCCRtPath,
126 GCCInstallation.getInstallPath(), MultilibSuf);
127 addPathIfExists(D, GCCRtPath, getFilePaths());
128 }
129
130 SmallString<128> SysRootDir(computeSysRoot());
131 llvm::sys::path::append(SysRootDir, "lib", MultilibSuf);
132 addPathIfExists(D, SysRootDir, getFilePaths());
133}
134
135std::string MSP430ToolChain::computeSysRoot() const {
136 if (!getDriver().SysRoot.empty())
137 return getDriver().SysRoot;
138
139 SmallString<128> Dir;
140 if (GCCInstallation.isValid())
141 llvm::sys::path::append(Dir, GCCInstallation.getParentLibPath(), "..",
142 GCCInstallation.getTriple().str());
143 else
144 llvm::sys::path::append(Dir, getDriver().Dir, "..", getTriple().str());
145
146 return Dir.str();
147}
148
149void MSP430ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
150 ArgStringList &CC1Args) const {
151 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
152 DriverArgs.hasArg(options::OPT_nostdlibinc))
153 return;
154
155 SmallString<128> Dir(computeSysRoot());
156 llvm::sys::path::append(Dir, "include");
157 addSystemInclude(DriverArgs, CC1Args, Dir.str());
158}
159
160void MSP430ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
161 ArgStringList &CC1Args,
162 Action::OffloadKind) const {
163 CC1Args.push_back("-nostdsysteminc");
164
165 const auto *MCUArg = DriverArgs.getLastArg(options::OPT_mmcu_EQ);
166 if (!MCUArg)
167 return;
168
169 const StringRef MCU = MCUArg->getValue();
170 if (MCU.startswith("msp430i")) {
171 // 'i' should be in lower case as it's defined in TI MSP430-GCC headers
172 CC1Args.push_back(DriverArgs.MakeArgString(
173 "-D__MSP430i" + MCU.drop_front(7).upper() + "__"));
174 } else {
175 CC1Args.push_back(DriverArgs.MakeArgString("-D__" + MCU.upper() + "__"));
176 }
177}
178
179Tool *MSP430ToolChain::buildLinker() const {
180 return new tools::msp430::Linker(*this);
181}
182
183void msp430::Linker::ConstructJob(Compilation &C, const JobAction &JA,
184 const InputInfo &Output,
185 const InputInfoList &Inputs,
186 const ArgList &Args,
187 const char *LinkingOutput) const {
188 const ToolChain &ToolChain = getToolChain();
189 const Driver &D = ToolChain.getDriver();
190 std::string Linker = ToolChain.GetProgramPath(getShortName());
191 ArgStringList CmdArgs;
192
193 if (!D.SysRoot.empty())
194 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
195
196 Args.AddAllArgs(CmdArgs, options::OPT_L);
197 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
198
199 if (!Args.hasArg(options::OPT_T)) {
200 if (const Arg *MCUArg = Args.getLastArg(options::OPT_mmcu_EQ))
201 CmdArgs.push_back(
202 Args.MakeArgString("-T" + StringRef(MCUArg->getValue()) + ".ld"));
203 } else {
204 Args.AddAllArgs(CmdArgs, options::OPT_T);
205 }
206
207 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
208 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
209 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
210 }
211
212 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
213
214 CmdArgs.push_back("--start-group");
215 CmdArgs.push_back(Args.MakeArgString(getHWMultLib(Args)));
216 CmdArgs.push_back("-lgcc");
217 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
218 CmdArgs.push_back("-lc");
219 CmdArgs.push_back("-lcrt");
220 CmdArgs.push_back("-lnosys");
221 }
222 CmdArgs.push_back("--end-group");
223
224 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
225 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
226 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
227 }
228 CmdArgs.push_back("-o");
229 CmdArgs.push_back(Output.getFilename());
230 C.addCommand(std::make_unique<Command>(JA, *this, Args.MakeArgString(Linker),
231 CmdArgs, Inputs));
232}