testDebuggerVariablesHelper.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include <memory>
  4. #include <set>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #include <cm3p/cppdap/optional.h>
  9. #include <cm3p/cppdap/protocol.h>
  10. #include <cm3p/cppdap/types.h>
  11. #include <stddef.h>
  12. #include <stdint.h>
  13. #include "cmDebuggerStackFrame.h"
  14. #include "cmDebuggerVariables.h"
  15. #include "cmDebuggerVariablesHelper.h"
  16. #include "cmDebuggerVariablesManager.h"
  17. #include "cmFileSet.h"
  18. #include "cmGlobalGenerator.h"
  19. #include "cmListFileCache.h"
  20. #include "cmMakefile.h"
  21. #include "cmPolicies.h"
  22. #include "cmPropertyMap.h"
  23. #include "cmState.h"
  24. #include "cmStateDirectory.h"
  25. #include "cmStateSnapshot.h"
  26. #include "cmStateTypes.h"
  27. #include "cmTarget.h"
  28. #include "cmTest.h"
  29. #include "cmake.h"
  30. #include "testCommon.h"
  31. #include "testDebugger.h"
  32. static dap::VariablesRequest CreateVariablesRequest(int64_t reference)
  33. {
  34. dap::VariablesRequest variableRequest;
  35. variableRequest.variablesReference = reference;
  36. return variableRequest;
  37. }
  38. struct Dummies
  39. {
  40. std::shared_ptr<cmake> CMake;
  41. std::shared_ptr<cmMakefile> Makefile;
  42. std::shared_ptr<cmGlobalGenerator> GlobalGenerator;
  43. };
  44. static Dummies CreateDummies(
  45. std::string targetName,
  46. std::string currentSourceDirectory = "c:/CurrentSourceDirectory",
  47. std::string currentBinaryDirectory = "c:/CurrentBinaryDirectory")
  48. {
  49. Dummies dummies;
  50. dummies.CMake = std::make_shared<cmake>(cmState::Role::Project);
  51. cmState* state = dummies.CMake->GetState();
  52. dummies.GlobalGenerator =
  53. std::make_shared<cmGlobalGenerator>(dummies.CMake.get());
  54. cmStateSnapshot snapshot = state->CreateBaseSnapshot();
  55. snapshot.GetDirectory().SetCurrentSource(currentSourceDirectory);
  56. snapshot.GetDirectory().SetCurrentBinary(currentBinaryDirectory);
  57. dummies.Makefile =
  58. std::make_shared<cmMakefile>(dummies.GlobalGenerator.get(), snapshot);
  59. dummies.Makefile->CreateNewTarget(targetName, cmStateEnums::EXECUTABLE);
  60. return dummies;
  61. }
  62. static bool testCreateFromPolicyMap()
  63. {
  64. auto variablesManager =
  65. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  66. cmPolicies::PolicyMap policyMap;
  67. policyMap.Set(cmPolicies::CMP0178, cmPolicies::NEW);
  68. policyMap.Set(cmPolicies::CMP0179, cmPolicies::WARN);
  69. policyMap.Set(cmPolicies::CMP0180, cmPolicies::OLD);
  70. auto vars = cmDebugger::cmDebuggerVariablesHelper::Create(
  71. variablesManager, "Locals", true, policyMap);
  72. dap::array<dap::Variable> variables =
  73. variablesManager->HandleVariablesRequest(
  74. CreateVariablesRequest(vars->GetId()));
  75. ASSERT_TRUE(variables.size() == 3);
  76. ASSERT_VARIABLE(variables[0], "CMP0178", "NEW", "string");
  77. ASSERT_VARIABLE(variables[1], "CMP0179", "WARN", "string");
  78. ASSERT_VARIABLE(variables[2], "CMP0180", "OLD", "string");
  79. return true;
  80. }
  81. static bool testCreateFromPairVector()
  82. {
  83. auto variablesManager =
  84. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  85. std::vector<std::pair<std::string, std::string>> pairs = {
  86. { "Foo1", "Bar1" }, { "Foo2", "Bar2" }
  87. };
  88. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  89. variablesManager, "Locals", true, pairs);
  90. dap::array<dap::Variable> variables =
  91. variablesManager->HandleVariablesRequest(
  92. CreateVariablesRequest(vars->GetId()));
  93. ASSERT_TRUE(vars->GetValue() == std::to_string(pairs.size()));
  94. ASSERT_TRUE(variables.size() == 2);
  95. ASSERT_VARIABLE(variables[0], "Foo1", "Bar1", "string");
  96. ASSERT_VARIABLE(variables[1], "Foo2", "Bar2", "string");
  97. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  98. variablesManager, "Locals", true,
  99. std::vector<std::pair<std::string, std::string>>());
  100. ASSERT_TRUE(none == nullptr);
  101. return true;
  102. }
  103. static bool testCreateFromSet()
  104. {
  105. auto variablesManager =
  106. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  107. std::set<std::string> set = { "Foo", "Bar" };
  108. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  109. variablesManager, "Locals", true, set);
  110. dap::array<dap::Variable> variables =
  111. variablesManager->HandleVariablesRequest(
  112. CreateVariablesRequest(vars->GetId()));
  113. ASSERT_TRUE(vars->GetValue() == std::to_string(set.size()));
  114. ASSERT_TRUE(variables.size() == 2);
  115. ASSERT_VARIABLE(variables[0], "[0]", "Bar", "string");
  116. ASSERT_VARIABLE(variables[1], "[1]", "Foo", "string");
  117. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  118. variablesManager, "Locals", true, std::set<std::string>());
  119. ASSERT_TRUE(none == nullptr);
  120. return true;
  121. }
  122. static bool testCreateFromStringVector()
  123. {
  124. auto variablesManager =
  125. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  126. std::vector<std::string> list = { "Foo", "Bar" };
  127. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  128. variablesManager, "Locals", true, list);
  129. dap::array<dap::Variable> variables =
  130. variablesManager->HandleVariablesRequest(
  131. CreateVariablesRequest(vars->GetId()));
  132. ASSERT_TRUE(vars->GetValue() == std::to_string(list.size()));
  133. ASSERT_TRUE(variables.size() == 2);
  134. ASSERT_VARIABLE(variables[0], "[0]", "Foo", "string");
  135. ASSERT_VARIABLE(variables[1], "[1]", "Bar", "string");
  136. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  137. variablesManager, "Locals", true, std::vector<std::string>());
  138. ASSERT_TRUE(none == nullptr);
  139. return true;
  140. }
  141. static bool testCreateFromTarget()
  142. {
  143. auto variablesManager =
  144. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  145. auto dummies = CreateDummies("Foo");
  146. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  147. variablesManager, "Locals", true, dummies.Makefile->GetOrderedTargets());
  148. dap::array<dap::Variable> variables =
  149. variablesManager->HandleVariablesRequest(
  150. CreateVariablesRequest(vars->GetId()));
  151. ASSERT_TRUE(variables.size() == 1);
  152. ASSERT_VARIABLE(variables[0], "Foo", "EXECUTABLE", "collection");
  153. variables = variablesManager->HandleVariablesRequest(
  154. CreateVariablesRequest(variables[0].variablesReference));
  155. ASSERT_TRUE(variables.size() == 15);
  156. ASSERT_VARIABLE(variables[0], "GlobalGenerator", "Generic", "collection");
  157. ASSERT_VARIABLE(variables[1], "IsAIX", "FALSE", "bool");
  158. ASSERT_VARIABLE(variables[2], "IsAndroidGuiExecutable", "FALSE", "bool");
  159. ASSERT_VARIABLE(variables[3], "IsAppBundleOnApple", "FALSE", "bool");
  160. ASSERT_VARIABLE(variables[4], "IsDLLPlatform", "FALSE", "bool");
  161. ASSERT_VARIABLE(variables[5], "IsExecutableWithExports", "FALSE", "bool");
  162. ASSERT_VARIABLE(variables[6], "IsFrameworkOnApple", "FALSE", "bool");
  163. ASSERT_VARIABLE(variables[7], "IsImported", "FALSE", "bool");
  164. ASSERT_VARIABLE(variables[8], "IsImportedGloballyVisible", "FALSE", "bool");
  165. ASSERT_VARIABLE(variables[9], "IsPerConfig", "TRUE", "bool");
  166. ASSERT_VARIABLE(variables[10], "Makefile",
  167. dummies.Makefile->GetDirectoryId().String, "collection");
  168. ASSERT_VARIABLE(variables[11], "Name", "Foo", "string");
  169. ASSERT_VARIABLE(variables[12], "PolicyMap", "", "collection");
  170. ASSERT_VARIABLE(variables[13], "Properties",
  171. std::to_string(dummies.Makefile->GetOrderedTargets()[0]
  172. ->GetProperties()
  173. .GetList()
  174. .size()),
  175. "collection");
  176. ASSERT_VARIABLE(variables[14], "Type", "EXECUTABLE", "string");
  177. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  178. variablesManager, "Locals", true, std::vector<cmTarget*>());
  179. ASSERT_TRUE(none == nullptr);
  180. return true;
  181. }
  182. static bool testCreateFromGlobalGenerator()
  183. {
  184. auto variablesManager =
  185. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  186. auto dummies = CreateDummies("Foo");
  187. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  188. variablesManager, "Locals", true, dummies.GlobalGenerator.get());
  189. dap::array<dap::Variable> variables =
  190. variablesManager->HandleVariablesRequest(
  191. CreateVariablesRequest(vars->GetId()));
  192. ASSERT_TRUE(variables.size() == 10);
  193. ASSERT_VARIABLE(variables[0], "AllTargetName", "ALL_BUILD", "string");
  194. ASSERT_VARIABLE(variables[1], "ForceUnixPaths", "FALSE", "bool");
  195. ASSERT_VARIABLE(variables[2], "InstallTargetName", "INSTALL", "string");
  196. ASSERT_VARIABLE(variables[3], "IsMultiConfig", "FALSE", "bool");
  197. ASSERT_VARIABLE(variables[4], "MakefileEncoding", "None", "string");
  198. ASSERT_VARIABLE(variables[5], "Name", "Generic", "string");
  199. ASSERT_VARIABLE(variables[6], "NeedSymbolicMark", "FALSE", "bool");
  200. ASSERT_VARIABLE(variables[7], "PackageTargetName", "PACKAGE", "string");
  201. ASSERT_VARIABLE(variables[8], "TestTargetName", "RUN_TESTS", "string");
  202. ASSERT_VARIABLE(variables[9], "UseLinkScript", "FALSE", "bool");
  203. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  204. variablesManager, "Locals", true,
  205. static_cast<cmGlobalGenerator*>(nullptr));
  206. ASSERT_TRUE(none == nullptr);
  207. return true;
  208. }
  209. static bool testCreateFromTests()
  210. {
  211. auto variablesManager =
  212. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  213. auto dummies = CreateDummies("Foo");
  214. cmTest test1 = cmTest(dummies.Makefile.get());
  215. test1.SetName("Test1");
  216. test1.SetOldStyle(false);
  217. test1.SetCommandExpandLists(true);
  218. test1.SetCommand(std::vector<std::string>{ "Foo1", "arg1" });
  219. test1.SetProperty("Prop1", "Prop1");
  220. cmTest test2 = cmTest(dummies.Makefile.get());
  221. test2.SetName("Test2");
  222. test2.SetOldStyle(false);
  223. test2.SetCommandExpandLists(false);
  224. test2.SetCommand(std::vector<std::string>{ "Bar1", "arg1", "arg2" });
  225. test2.SetProperty("Prop2", "Prop2");
  226. test2.SetProperty("Prop3", "Prop3");
  227. std::vector<cmTest*> tests = { &test1, &test2 };
  228. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  229. variablesManager, "Locals", true, tests);
  230. dap::array<dap::Variable> variables =
  231. variablesManager->HandleVariablesRequest(
  232. CreateVariablesRequest(vars->GetId()));
  233. ASSERT_TRUE(vars->GetValue() == std::to_string(tests.size()));
  234. ASSERT_TRUE(variables.size() == 2);
  235. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(variables[0], test1.GetName(), "",
  236. "collection");
  237. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(variables[1], test2.GetName(), "",
  238. "collection");
  239. dap::array<dap::Variable> testVariables =
  240. variablesManager->HandleVariablesRequest(
  241. CreateVariablesRequest(variables[0].variablesReference));
  242. ASSERT_TRUE(testVariables.size() == 5);
  243. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(testVariables[0], "Command",
  244. std::to_string(test1.GetCommand().size()),
  245. "collection");
  246. ASSERT_VARIABLE(testVariables[1], "CommandExpandLists",
  247. BOOL_STRING(test1.GetCommandExpandLists()), "bool");
  248. ASSERT_VARIABLE(testVariables[2], "Name", test1.GetName(), "string");
  249. ASSERT_VARIABLE(testVariables[3], "OldStyle",
  250. BOOL_STRING(test1.GetOldStyle()), "bool");
  251. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(testVariables[4], "Properties", "1",
  252. "collection");
  253. dap::array<dap::Variable> commandVariables =
  254. variablesManager->HandleVariablesRequest(
  255. CreateVariablesRequest(testVariables[0].variablesReference));
  256. ASSERT_TRUE(commandVariables.size() == test1.GetCommand().size());
  257. for (size_t i = 0; i < commandVariables.size(); ++i) {
  258. ASSERT_VARIABLE(commandVariables[i], "[" + std::to_string(i) + "]",
  259. test1.GetCommand()[i], "string");
  260. }
  261. dap::array<dap::Variable> propertiesVariables =
  262. variablesManager->HandleVariablesRequest(
  263. CreateVariablesRequest(testVariables[4].variablesReference));
  264. ASSERT_TRUE(propertiesVariables.size() == 1);
  265. ASSERT_VARIABLE(propertiesVariables[0], "Prop1", "Prop1", "string");
  266. testVariables = variablesManager->HandleVariablesRequest(
  267. CreateVariablesRequest(variables[1].variablesReference));
  268. ASSERT_TRUE(testVariables.size() == 5);
  269. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(testVariables[0], "Command",
  270. std::to_string(test2.GetCommand().size()),
  271. "collection");
  272. ASSERT_VARIABLE(testVariables[1], "CommandExpandLists",
  273. BOOL_STRING(test2.GetCommandExpandLists()), "bool");
  274. ASSERT_VARIABLE(testVariables[2], "Name", test2.GetName(), "string");
  275. ASSERT_VARIABLE(testVariables[3], "OldStyle",
  276. BOOL_STRING(test2.GetOldStyle()), "bool");
  277. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(testVariables[4], "Properties", "2",
  278. "collection");
  279. commandVariables = variablesManager->HandleVariablesRequest(
  280. CreateVariablesRequest(testVariables[0].variablesReference));
  281. ASSERT_TRUE(commandVariables.size() == test2.GetCommand().size());
  282. for (size_t i = 0; i < commandVariables.size(); ++i) {
  283. ASSERT_VARIABLE(commandVariables[i], "[" + std::to_string(i) + "]",
  284. test2.GetCommand()[i], "string");
  285. }
  286. propertiesVariables = variablesManager->HandleVariablesRequest(
  287. CreateVariablesRequest(testVariables[4].variablesReference));
  288. ASSERT_TRUE(propertiesVariables.size() == 2);
  289. ASSERT_VARIABLE(propertiesVariables[0], "Prop2", "Prop2", "string");
  290. ASSERT_VARIABLE(propertiesVariables[1], "Prop3", "Prop3", "string");
  291. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  292. variablesManager, "Locals", true, std::vector<cmTest*>());
  293. ASSERT_TRUE(none == nullptr);
  294. return true;
  295. }
  296. static bool testCreateFromMakefile()
  297. {
  298. auto variablesManager =
  299. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  300. auto dummies = CreateDummies("Foo");
  301. auto snapshot = dummies.Makefile->GetStateSnapshot();
  302. auto state = dummies.Makefile->GetState();
  303. state->SetSourceDirectory("c:/HomeDirectory");
  304. state->SetBinaryDirectory("c:/HomeOutputDirectory");
  305. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  306. variablesManager, "Locals", true, dummies.Makefile.get());
  307. dap::array<dap::Variable> variables =
  308. variablesManager->HandleVariablesRequest(
  309. CreateVariablesRequest(vars->GetId()));
  310. ASSERT_TRUE(variables.size() == 12);
  311. ASSERT_VARIABLE(variables[0], "AppleSDKType", "MacOS", "string");
  312. ASSERT_VARIABLE(variables[1], "CurrentBinaryDirectory",
  313. snapshot.GetDirectory().GetCurrentBinary(), "string");
  314. ASSERT_VARIABLE(variables[2], "CurrentSourceDirectory",
  315. snapshot.GetDirectory().GetCurrentSource(), "string");
  316. ASSERT_VARIABLE(variables[3], "DefineFlags", " ", "string");
  317. ASSERT_VARIABLE(variables[4], "DirectoryId",
  318. dummies.Makefile->GetDirectoryId().String, "string");
  319. ASSERT_VARIABLE(variables[5], "HomeDirectory", state->GetSourceDirectory(),
  320. "string");
  321. ASSERT_VARIABLE(variables[6], "HomeOutputDirectory",
  322. state->GetBinaryDirectory(), "string");
  323. ASSERT_VARIABLE(variables[7], "IsRootMakefile", "TRUE", "bool");
  324. ASSERT_VARIABLE(variables[8], "PlatformIs32Bit", "FALSE", "bool");
  325. ASSERT_VARIABLE(variables[9], "PlatformIs64Bit", "FALSE", "bool");
  326. ASSERT_VARIABLE(variables[10], "PlatformIsAppleEmbedded", "FALSE", "bool");
  327. ASSERT_VARIABLE(variables[11], "PlatformIsx32", "FALSE", "bool");
  328. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  329. variablesManager, "Locals", true, static_cast<cmMakefile*>(nullptr));
  330. ASSERT_TRUE(none == nullptr);
  331. return true;
  332. }
  333. static bool testCreateFromStackFrame()
  334. {
  335. auto variablesManager =
  336. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  337. auto dummies = CreateDummies("Foo");
  338. cmListFileFunction lff = cmListFileFunction("set", 99, 99, {});
  339. auto frame = std::make_shared<cmDebugger::cmDebuggerStackFrame>(
  340. dummies.Makefile.get(), "c:/CMakeLists.txt", lff);
  341. dummies.CMake->AddCacheEntry("CMAKE_BUILD_TYPE", "Debug", "Build Type",
  342. cmStateEnums::CacheEntryType::STRING);
  343. auto locals = cmDebugger::cmDebuggerVariablesHelper::Create(
  344. variablesManager, "Locals", true, frame);
  345. dap::array<dap::Variable> variables =
  346. variablesManager->HandleVariablesRequest(
  347. CreateVariablesRequest(locals->GetId()));
  348. ASSERT_TRUE(variables.size() == 5);
  349. ASSERT_VARIABLE(variables[0], "CacheVariables", "1", "collection");
  350. ASSERT_VARIABLE(variables[1], "CurrentLine", std::to_string(lff.Line()),
  351. "int");
  352. ASSERT_VARIABLE(variables[2], "Directories", "2", "collection");
  353. ASSERT_VARIABLE(variables[3], "Locals", "2", "collection");
  354. ASSERT_VARIABLE(variables[4], "Targets", "1", "collection");
  355. dap::array<dap::Variable> cacheVariables =
  356. variablesManager->HandleVariablesRequest(
  357. CreateVariablesRequest(variables[0].variablesReference));
  358. ASSERT_TRUE(cacheVariables.size() == 1);
  359. ASSERT_VARIABLE(cacheVariables[0], "CMAKE_BUILD_TYPE:STRING", "Debug",
  360. "collection");
  361. dap::array<dap::Variable> directoriesVariables =
  362. variablesManager->HandleVariablesRequest(
  363. CreateVariablesRequest(variables[2].variablesReference));
  364. ASSERT_TRUE(directoriesVariables.size() == 2);
  365. ASSERT_VARIABLE(
  366. directoriesVariables[0], "CMAKE_CURRENT_BINARY_DIR",
  367. dummies.Makefile->GetStateSnapshot().GetDirectory().GetCurrentBinary(),
  368. "string");
  369. ASSERT_VARIABLE(
  370. directoriesVariables[1], "CMAKE_CURRENT_SOURCE_DIR",
  371. dummies.Makefile->GetStateSnapshot().GetDirectory().GetCurrentSource(),
  372. "string");
  373. dap::array<dap::Variable> propertiesVariables =
  374. variablesManager->HandleVariablesRequest(
  375. CreateVariablesRequest(cacheVariables[0].variablesReference));
  376. ASSERT_TRUE(propertiesVariables.size() == 3);
  377. ASSERT_VARIABLE(propertiesVariables[0], "HELPSTRING", "Build Type",
  378. "string");
  379. ASSERT_VARIABLE(propertiesVariables[1], "TYPE", "STRING", "string");
  380. ASSERT_VARIABLE(propertiesVariables[2], "VALUE", "Debug", "string");
  381. return true;
  382. }
  383. static bool testCreateFromBTStringVector()
  384. {
  385. auto variablesManager =
  386. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  387. std::vector<BT<std::string>> list(2);
  388. list[0].Value = "Foo";
  389. list[1].Value = "Bar";
  390. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  391. variablesManager, "Locals", true, list);
  392. dap::array<dap::Variable> variables =
  393. variablesManager->HandleVariablesRequest(
  394. CreateVariablesRequest(vars->GetId()));
  395. ASSERT_TRUE(vars->GetValue() == std::to_string(list.size()));
  396. ASSERT_TRUE(variables.size() == 2);
  397. ASSERT_VARIABLE(variables[0], "[0]", "Foo", "string");
  398. ASSERT_VARIABLE(variables[1], "[1]", "Bar", "string");
  399. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  400. variablesManager, "Locals", true, std::vector<std::string>());
  401. ASSERT_TRUE(none == nullptr);
  402. return true;
  403. }
  404. static bool testCreateFromFileSet()
  405. {
  406. auto variablesManager =
  407. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  408. cmake cm(cmState::Role::Internal);
  409. cmFileSet fileSet(cm, "Foo", "HEADERS", cmFileSetVisibility::Public);
  410. BT<std::string> directory;
  411. directory.Value = "c:/";
  412. fileSet.AddDirectoryEntry(directory);
  413. BT<std::string> file;
  414. file.Value = "c:/foo.cxx";
  415. fileSet.AddFileEntry(file);
  416. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  417. variablesManager, "Locals", true, &fileSet);
  418. dap::array<dap::Variable> variables =
  419. variablesManager->HandleVariablesRequest(
  420. CreateVariablesRequest(vars->GetId()));
  421. ASSERT_TRUE(variables.size() == 5);
  422. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(variables[0], "Directories", "1",
  423. "collection");
  424. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(variables[1], "Files", "1", "collection");
  425. ASSERT_VARIABLE(variables[2], "Name", "Foo", "string");
  426. ASSERT_VARIABLE(variables[3], "Type", "HEADERS", "string");
  427. ASSERT_VARIABLE(variables[4], "Visibility", "Public", "string");
  428. dap::array<dap::Variable> directoriesVariables =
  429. variablesManager->HandleVariablesRequest(
  430. CreateVariablesRequest(variables[0].variablesReference));
  431. ASSERT_TRUE(directoriesVariables.size() == 1);
  432. ASSERT_VARIABLE(directoriesVariables[0], "[0]", directory.Value, "string");
  433. dap::array<dap::Variable> filesVariables =
  434. variablesManager->HandleVariablesRequest(
  435. CreateVariablesRequest(variables[1].variablesReference));
  436. ASSERT_TRUE(filesVariables.size() == 1);
  437. ASSERT_VARIABLE(filesVariables[0], "[0]", file.Value, "string");
  438. return true;
  439. }
  440. static bool testCreateFromFileSets()
  441. {
  442. auto variablesManager =
  443. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  444. cmake cm(cmState::Role::Internal);
  445. cmFileSet fileSet(cm, "Foo", "HEADERS", cmFileSetVisibility::Public);
  446. BT<std::string> directory;
  447. directory.Value = "c:/";
  448. fileSet.AddDirectoryEntry(directory);
  449. BT<std::string> file;
  450. file.Value = "c:/foo.cxx";
  451. fileSet.AddFileEntry(file);
  452. auto fileSets = std::vector<cmFileSet*>{ &fileSet };
  453. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  454. variablesManager, "Locals", true, fileSets);
  455. dap::array<dap::Variable> variables =
  456. variablesManager->HandleVariablesRequest(
  457. CreateVariablesRequest(vars->GetId()));
  458. ASSERT_TRUE(variables.size() == 1);
  459. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(variables[0], "Foo", "", "collection");
  460. return true;
  461. }
  462. int testDebuggerVariablesHelper(int, char*[])
  463. {
  464. return runTests({ testCreateFromPolicyMap, testCreateFromPairVector,
  465. testCreateFromSet, testCreateFromStringVector,
  466. testCreateFromTarget, testCreateFromGlobalGenerator,
  467. testCreateFromMakefile, testCreateFromStackFrame,
  468. testCreateFromTests, testCreateFromBTStringVector,
  469. testCreateFromFileSet, testCreateFromFileSets });
  470. }