DXIsenseTest.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DXIsenseTest.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Provides tests for the dxcompiler Intellisense API. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "CompilationResult.h"
  12. #include "HLSLTestData.h"
  13. #include <stdint.h>
  14. #include "WexTestClass.h"
  15. #include "HlslTestUtils.h"
  16. #include "dxc/Support/microcom.h"
  17. class DXIntellisenseTest
  18. {
  19. public:
  20. BEGIN_TEST_CLASS(DXIntellisenseTest)
  21. TEST_CLASS_PROPERTY(L"Parallel", L"true")
  22. TEST_METHOD_PROPERTY(L"Priority", L"0")
  23. END_TEST_CLASS()
  24. protected:
  25. TEST_CLASS_SETUP(DXIntellisenseTestClassSetup);
  26. TEST_CLASS_CLEANUP(DXIntellisenseTestClassCleanup);
  27. void GetLocationAt(IDxcTranslationUnit* TU, unsigned line, unsigned col, IDxcSourceLocation** pResult)
  28. {
  29. CComPtr<IDxcFile> file;
  30. VERIFY_SUCCEEDED(TU->GetFile("filename.hlsl", &file));
  31. VERIFY_SUCCEEDED(TU->GetLocation(file, line, col, pResult));
  32. }
  33. void GetCursorAt(IDxcTranslationUnit* TU, unsigned line, unsigned col, IDxcCursor** pResult)
  34. {
  35. CComPtr<IDxcSourceLocation> location;
  36. GetLocationAt(TU, line, col, &location);
  37. VERIFY_SUCCEEDED(TU->GetCursorForLocation(location, pResult));
  38. }
  39. void ExpectCursorAt(IDxcTranslationUnit* TU, unsigned line, unsigned col,
  40. DxcCursorKind expectedKind, _COM_Outptr_opt_ IDxcCursor** pResult = nullptr)
  41. {
  42. CComPtr<IDxcCursor> cursor;
  43. DxcCursorKind actualKind;
  44. GetCursorAt(TU, line, col, &cursor);
  45. VERIFY_SUCCEEDED(cursor->GetKind(&actualKind));
  46. EXPECT_EQ(expectedKind, actualKind);// << " for cursor at " << line << ":" << col;
  47. if (pResult != nullptr)
  48. {
  49. *pResult = cursor.Detach();
  50. }
  51. }
  52. void ExpectQualifiedName(IDxcTranslationUnit* TU, unsigned line, unsigned col, const wchar_t* expectedName)
  53. {
  54. CComPtr<IDxcCursor> cursor;
  55. CComBSTR name;
  56. GetCursorAt(TU, line, col, &cursor);
  57. ASSERT_HRESULT_SUCCEEDED(cursor->GetQualifiedName(FALSE, &name));
  58. EXPECT_STREQW(expectedName, name);// << "qualified name at " << line << ":" << col;
  59. }
  60. void ExpectDeclarationText(IDxcTranslationUnit* TU, unsigned line, unsigned col, const wchar_t* expectedDecl)
  61. {
  62. CComPtr<IDxcCursor> cursor;
  63. CComBSTR name;
  64. GetCursorAt(TU, line, col, &cursor);
  65. DxcCursorFormatting formatting = (DxcCursorFormatting)(
  66. DxcCursorFormatting_IncludeNamespaceKeyword);
  67. ASSERT_HRESULT_SUCCEEDED(cursor->GetFormattedName(formatting, &name));
  68. EXPECT_STREQW(expectedDecl, name);// << "declaration text at " << line << ":" << col;
  69. }
  70. TEST_METHOD(CursorWhenCBufferRefThenFound);
  71. TEST_METHOD(CursorWhenFieldRefThenSimpleNames);
  72. TEST_METHOD(CursorWhenFindAtBodyCallThenMatch);
  73. TEST_METHOD(CursorWhenFindAtGlobalThenMatch);
  74. TEST_METHOD(CursorWhenFindBeforeBodyCallThenMatch);
  75. TEST_METHOD(CursorWhenFindBeforeGlobalThenMatch);
  76. TEST_METHOD(CursorWhenFunctionThenParamsAvailable);
  77. TEST_METHOD(CursorWhenFunctionThenReturnTypeAvailable);
  78. TEST_METHOD(CursorWhenFunctionThenSignatureAvailable);
  79. TEST_METHOD(CursorWhenGlobalVariableThenSimpleNames);
  80. TEST_METHOD(CursorWhenOverloadedIncompleteThenInvisible);
  81. TEST_METHOD(CursorWhenOverloadedResolvedThenDirectSymbol);
  82. TEST_METHOD(CursorWhenReferenceThenDefinitionAvailable);
  83. TEST_METHOD(CursorWhenTypeOfVariableDeclThenNamesHaveType);
  84. TEST_METHOD(CursorWhenVariableRefThenSimpleNames);
  85. TEST_METHOD(CursorWhenVariableUsedThenDeclarationAvailable);
  86. TEST_METHOD(FileWhenSameThenEqual);
  87. TEST_METHOD(FileWhenNotSameThenNotEqual);
  88. TEST_METHOD(InclusionWhenValidThenAvailable);
  89. TEST_METHOD(TUWhenGetFileMissingThenFail);
  90. TEST_METHOD(TUWhenGetFilePresentThenOK);
  91. TEST_METHOD(TUWhenEmptyStructThenErrorIfISense);
  92. TEST_METHOD(TUWhenRegionInactiveMissingThenCountIsZero);
  93. TEST_METHOD(TUWhenRegionInactiveThenEndIsBeforeElseHash);
  94. TEST_METHOD(TUWhenRegionInactiveThenEndIsBeforeEndifHash);
  95. TEST_METHOD(TUWhenRegionInactiveThenStartIsAtIfdefEol);
  96. TEST_METHOD(TUWhenUnsaveFileThenOK);
  97. TEST_METHOD(QualifiedNameClass);
  98. TEST_METHOD(QualifiedNameVariable);
  99. TEST_METHOD(TypeWhenICEThenEval);
  100. };
  101. std::shared_ptr<HlslIntellisenseSupport> CompilationResult::DefaultHlslSupport;
  102. bool DXIntellisenseTest::DXIntellisenseTestClassSetup() {
  103. std::shared_ptr<HlslIntellisenseSupport> result = std::make_shared<HlslIntellisenseSupport>();
  104. if (FAILED(result->Initialize()))
  105. return false;
  106. CompilationResult::DefaultHlslSupport = result;
  107. return true;
  108. }
  109. bool DXIntellisenseTest::DXIntellisenseTestClassCleanup() {
  110. CompilationResult::DefaultHlslSupport = nullptr;
  111. return true;
  112. }
  113. TEST_F(DXIntellisenseTest, CursorWhenCBufferRefThenFound) {
  114. char program[] =
  115. "cbuffer MyBuffer {\r\n"
  116. " int a; }\r\n"
  117. "int main() { return\r\n"
  118. "a; }";
  119. CComPtr<IDxcCursor> varRefCursor;
  120. CComPtr<IDxcFile> file;
  121. CComInterfaceArray<IDxcCursor> refs;
  122. CComPtr<IDxcSourceLocation> loc;
  123. unsigned line;
  124. CompilationResult c(CompilationResult::CreateForProgram(program, strlen(program)));
  125. VERIFY_ARE_EQUAL(true, c.ParseSucceeded());
  126. ExpectCursorAt(c.TU, 4, 1, DxcCursor_DeclRefExpr, &varRefCursor);
  127. VERIFY_SUCCEEDED(c.TU->GetFile(CompilationResult::getDefaultFileName(), &file));
  128. VERIFY_SUCCEEDED(varRefCursor->FindReferencesInFile(file, 0, 4, refs.size_ref(), refs.data_ref()));
  129. VERIFY_ARE_EQUAL(2, refs.size());
  130. VERIFY_SUCCEEDED(refs.begin()[0]->GetLocation(&loc));
  131. VERIFY_SUCCEEDED(loc->GetSpellingLocation(nullptr, &line, nullptr, nullptr));
  132. VERIFY_ARE_EQUAL(2, line);
  133. }
  134. TEST_F(DXIntellisenseTest, InclusionWhenValidThenAvailable) {
  135. CComPtr<IDxcIntelliSense> isense;
  136. CComPtr<IDxcIndex> index;
  137. CComPtr<IDxcUnsavedFile> unsaved[2];
  138. CComPtr<IDxcTranslationUnit> TU;
  139. CComInterfaceArray<IDxcInclusion> inclusions;
  140. const char main_text[] = "#include \"inc.h\"\r\nfloat4 main() : SV_Target { return FOO; }";
  141. const char unsaved_text[] = "#define FOO 1";
  142. unsigned diagCount;
  143. unsigned expectedIndex = 0;
  144. const char *expectedNames[2] = { "file.hlsl", "./inc.h" };
  145. VERIFY_SUCCEEDED(CompilationResult::DefaultHlslSupport->CreateIntellisense(&isense));
  146. VERIFY_SUCCEEDED(isense->CreateIndex(&index));
  147. VERIFY_SUCCEEDED(isense->CreateUnsavedFile("./inc.h", unsaved_text, strlen(unsaved_text), &unsaved[0]));
  148. VERIFY_SUCCEEDED(isense->CreateUnsavedFile("file.hlsl", main_text, strlen(main_text), &unsaved[1]));
  149. VERIFY_SUCCEEDED(index->ParseTranslationUnit("file.hlsl", nullptr, 0, &unsaved[0].p, 2,
  150. DxcTranslationUnitFlags_UseCallerThread, &TU));
  151. VERIFY_SUCCEEDED(TU->GetNumDiagnostics(&diagCount));
  152. VERIFY_ARE_EQUAL(0, diagCount);
  153. VERIFY_SUCCEEDED(TU->GetInclusionList(inclusions.size_ref(), inclusions.data_ref()));
  154. VERIFY_ARE_EQUAL(2, inclusions.size());
  155. for (IDxcInclusion * i : inclusions) {
  156. CComPtr<IDxcFile> file;
  157. CComHeapPtr<char> fileName;
  158. VERIFY_SUCCEEDED(i->GetIncludedFile(&file));
  159. VERIFY_SUCCEEDED(file->GetName(&fileName));
  160. VERIFY_ARE_EQUAL_STR(expectedNames[expectedIndex], fileName.m_pData);
  161. expectedIndex++;
  162. }
  163. }
  164. TEST_F(DXIntellisenseTest, TUWhenGetFileMissingThenFail) {
  165. const char program[] = "int i;";
  166. CompilationResult result = CompilationResult::CreateForProgram(program, strlen(program), nullptr);
  167. VERIFY_ARE_EQUAL(true, result.ParseSucceeded());
  168. CComPtr<IDxcFile> file;
  169. VERIFY_FAILED(result.TU->GetFile("unknonwn.txt", &file));
  170. }
  171. TEST_F(DXIntellisenseTest, TUWhenGetFilePresentThenOK) {
  172. const char program[] = "int i;";
  173. CompilationResult result = CompilationResult::CreateForProgram(program, strlen(program), nullptr);
  174. VERIFY_ARE_EQUAL(true, result.ParseSucceeded());
  175. CComPtr<IDxcFile> file;
  176. VERIFY_SUCCEEDED(result.TU->GetFile(CompilationResult::getDefaultFileName(), &file));
  177. VERIFY_IS_NOT_NULL(file.p);
  178. }
  179. TEST_F(DXIntellisenseTest, TUWhenEmptyStructThenErrorIfISense) {
  180. // An declaration of the from 'struct S;' is a forward declaration in HLSL
  181. // 2016, but is invalid in HLSL 2015.
  182. const char program[] = "struct S;";
  183. const char programWithDef[] = "struct S { int i; };";
  184. const char *args2015[] = { "-HV", "2015" };
  185. const char *args2016[] = { "-HV", "2016" };
  186. CompilationResult result15WithDef(
  187. CompilationResult::CreateForProgramAndArgs(programWithDef, _countof(programWithDef),
  188. args2015, _countof(args2015), nullptr));
  189. VERIFY_ARE_EQUAL(true, result15WithDef.ParseSucceeded());
  190. CompilationResult result15(
  191. CompilationResult::CreateForProgramAndArgs(program, _countof(program),
  192. args2015, _countof(args2015), nullptr));
  193. VERIFY_ARE_EQUAL(false, result15.ParseSucceeded());
  194. CompilationResult result16(
  195. CompilationResult::CreateForProgramAndArgs(program, _countof(program),
  196. args2016, _countof(args2016), nullptr));
  197. VERIFY_ARE_EQUAL(true, result16.ParseSucceeded());
  198. }
  199. TEST_F(DXIntellisenseTest, TUWhenRegionInactiveMissingThenCountIsZero) {
  200. char program[] = "void foo() { }";
  201. CompilationResult result(
  202. CompilationResult::CreateForProgram(program, _countof(program)));
  203. CComPtr<IDxcFile> file;
  204. unsigned resultCount;
  205. IDxcSourceRange** results;
  206. VERIFY_SUCCEEDED(result.TU->GetFile("filename.hlsl", &file));
  207. VERIFY_SUCCEEDED(result.TU->GetSkippedRanges(file.p, &resultCount, &results));
  208. VERIFY_ARE_EQUAL(0, resultCount);
  209. VERIFY_IS_NULL(results);
  210. }
  211. TEST_F(DXIntellisenseTest, TUWhenRegionInactiveThenEndIsBeforeElseHash) {
  212. char program[] =
  213. "#ifdef NOT // a comment\r\n"
  214. "int foo() { }\r\n"
  215. "#else // a comment\r\n"
  216. "int bar() { }\r\n"
  217. "#endif // a comment\r\n";
  218. DxcTranslationUnitFlags options = (DxcTranslationUnitFlags)
  219. (DxcTranslationUnitFlags_DetailedPreprocessingRecord | DxcTranslationUnitFlags_UseCallerThread);
  220. CompilationResult result(
  221. CompilationResult::CreateForProgram(program, _countof(program), &options));
  222. CComPtr<IDxcFile> file;
  223. unsigned resultCount;
  224. IDxcSourceRange** results;
  225. VERIFY_SUCCEEDED(result.TU->GetFile("filename.hlsl", &file));
  226. VERIFY_SUCCEEDED(result.TU->GetSkippedRanges(file.p, &resultCount, &results));
  227. ::WEX::TestExecution::DisableVerifyExceptions disable;
  228. VERIFY_ARE_EQUAL(1, resultCount);
  229. for (unsigned i = 0; i < resultCount; ++i)
  230. {
  231. CComPtr<IDxcSourceLocation> endLoc;
  232. VERIFY_SUCCEEDED(results[i]->GetEnd(&endLoc));
  233. unsigned line, col, offset;
  234. VERIFY_SUCCEEDED(endLoc->GetSpellingLocation(nullptr, &line, &col, &offset));
  235. VERIFY_ARE_EQUAL(3, line);
  236. VERIFY_ARE_EQUAL(1, col);
  237. results[i]->Release();
  238. }
  239. CoTaskMemFree(results);
  240. }
  241. TEST_F(DXIntellisenseTest, TUWhenRegionInactiveThenEndIsBeforeEndifHash) {
  242. char program[] =
  243. "#ifdef NOT // a comment\r\n"
  244. "int bar() { }\r\n"
  245. "#endif // a comment\r\n";
  246. DxcTranslationUnitFlags options = (DxcTranslationUnitFlags)
  247. (DxcTranslationUnitFlags_DetailedPreprocessingRecord | DxcTranslationUnitFlags_UseCallerThread);
  248. CompilationResult result(
  249. CompilationResult::CreateForProgram(program, _countof(program), &options));
  250. CComPtr<IDxcFile> file;
  251. unsigned resultCount;
  252. IDxcSourceRange** results;
  253. VERIFY_SUCCEEDED(result.TU->GetFile("filename.hlsl", &file));
  254. VERIFY_SUCCEEDED(result.TU->GetSkippedRanges(file.p, &resultCount, &results));
  255. ::WEX::TestExecution::DisableVerifyExceptions disable;
  256. VERIFY_ARE_EQUAL(1, resultCount);
  257. for (unsigned i = 0; i < resultCount; ++i)
  258. {
  259. CComPtr<IDxcSourceLocation> endLoc;
  260. VERIFY_SUCCEEDED(results[i]->GetEnd(&endLoc));
  261. unsigned line, col, offset;
  262. VERIFY_SUCCEEDED(endLoc->GetSpellingLocation(nullptr, &line, &col, &offset));
  263. VERIFY_ARE_EQUAL(3, line);
  264. VERIFY_ARE_EQUAL(1, col);
  265. results[i]->Release();
  266. }
  267. CoTaskMemFree(results);
  268. }
  269. TEST_F(DXIntellisenseTest, TUWhenRegionInactiveThenStartIsAtIfdefEol) {
  270. char program[] =
  271. "#ifdef NOT // a comment\r\n"
  272. "int foo() { }\r\n"
  273. "#else // a comment\r\n"
  274. "int bar() { }\r\n"
  275. "#endif // a comment\r\n";
  276. DxcTranslationUnitFlags options = (DxcTranslationUnitFlags)
  277. (DxcTranslationUnitFlags_DetailedPreprocessingRecord | DxcTranslationUnitFlags_UseCallerThread);
  278. CompilationResult result(
  279. CompilationResult::CreateForProgram(program, _countof(program), &options));
  280. CComPtr<IDxcFile> file;
  281. unsigned resultCount;
  282. IDxcSourceRange** results;
  283. VERIFY_SUCCEEDED(result.TU->GetFile("filename.hlsl", &file));
  284. VERIFY_SUCCEEDED(result.TU->GetSkippedRanges(file.p, &resultCount, &results));
  285. ::WEX::TestExecution::DisableVerifyExceptions disable;
  286. VERIFY_ARE_EQUAL(1, resultCount);
  287. for (unsigned i = 0; i < resultCount; ++i)
  288. {
  289. CComPtr<IDxcSourceLocation> startLoc;
  290. VERIFY_SUCCEEDED(results[i]->GetStart(&startLoc));
  291. unsigned line, col, offset;
  292. VERIFY_SUCCEEDED(startLoc->GetSpellingLocation(nullptr, &line, &col, &offset));
  293. VERIFY_ARE_EQUAL(1, line);
  294. VERIFY_ARE_EQUAL(24, col);
  295. results[i]->Release();
  296. }
  297. CoTaskMemFree(results);
  298. }
  299. std::ostream& operator<<(std::ostream& os, CComPtr<IDxcSourceLocation>& loc)
  300. {
  301. CComPtr<IDxcFile> locFile;
  302. unsigned locLine, locCol, locOffset;
  303. loc->GetSpellingLocation(&locFile, &locLine, &locCol, &locOffset);
  304. os << locLine << ':' << locCol << '@' << locOffset;
  305. return os;
  306. }
  307. std::wostream& operator<<(std::wostream& os, CComPtr<IDxcSourceLocation>& loc)
  308. {
  309. CComPtr<IDxcFile> locFile;
  310. unsigned locLine, locCol, locOffset;
  311. loc->GetSpellingLocation(&locFile, &locLine, &locCol, &locOffset);
  312. os << locLine << L':' << locCol << L'@' << locOffset;
  313. return os;
  314. }
  315. TEST_F(DXIntellisenseTest, TUWhenUnsaveFileThenOK) {
  316. // Verify that an unsaved file using the library-provided implementation still works.
  317. const char fileName[] = "filename.hlsl";
  318. char program[] =
  319. "[numthreads(1, 1, 1)]\r\n"
  320. "void main( uint3 DTid : SV_DispatchThreadID )\r\n"
  321. "{\r\n"
  322. "}";
  323. bool useBuiltInValues[] = { false, true };
  324. HlslIntellisenseSupport support;
  325. VERIFY_SUCCEEDED(support.Initialize());
  326. for (bool useBuiltIn : useBuiltInValues) {
  327. CComPtr<IDxcIntelliSense> isense;
  328. CComPtr<IDxcIndex> tuIndex;
  329. CComPtr<IDxcTranslationUnit> tu;
  330. CComPtr<IDxcUnsavedFile> unsavedFile;
  331. DxcTranslationUnitFlags localOptions;
  332. const char **commandLineArgs = nullptr;
  333. int commandLineArgsCount = 0;
  334. VERIFY_SUCCEEDED(support.CreateIntellisense(&isense));
  335. VERIFY_SUCCEEDED(isense->CreateIndex(&tuIndex));
  336. VERIFY_SUCCEEDED(isense->GetDefaultEditingTUOptions(&localOptions));
  337. if (useBuiltIn)
  338. VERIFY_SUCCEEDED(isense->CreateUnsavedFile(fileName, program, strlen(program), &unsavedFile));
  339. else
  340. VERIFY_SUCCEEDED(TrivialDxcUnsavedFile::Create(fileName, program, &unsavedFile));
  341. VERIFY_SUCCEEDED(tuIndex->ParseTranslationUnit(fileName,
  342. commandLineArgs, commandLineArgsCount,
  343. &(unsavedFile.p), 1, localOptions, &tu));
  344. // No errors expected.
  345. unsigned numDiagnostics;
  346. VERIFY_SUCCEEDED(tu->GetNumDiagnostics(&numDiagnostics));
  347. VERIFY_ARE_EQUAL(0, numDiagnostics);
  348. CComPtr<IDxcCursor> tuCursor;
  349. CComInterfaceArray<IDxcCursor> cursors;
  350. VERIFY_SUCCEEDED(tu->GetCursor(&tuCursor));
  351. VERIFY_SUCCEEDED(tuCursor->GetChildren(0, 20, cursors.size_ref(), cursors.data_ref()));
  352. std::wstringstream offsetStream;
  353. for (IDxcCursor *pCursor : cursors) {
  354. CComPtr<IDxcSourceRange> range;
  355. CComPtr<IDxcSourceLocation> location;
  356. CComPtr<IDxcSourceLocation> rangeStart, rangeEnd;
  357. CComBSTR name;
  358. VERIFY_SUCCEEDED(pCursor->GetExtent(&range));
  359. VERIFY_SUCCEEDED(range->GetStart(&rangeStart));
  360. VERIFY_SUCCEEDED(range->GetEnd(&rangeEnd));
  361. VERIFY_SUCCEEDED(pCursor->GetDisplayName(&name));
  362. VERIFY_SUCCEEDED(pCursor->GetLocation(&location));
  363. offsetStream << (LPWSTR)name << " - spelling " << location <<
  364. " - extent " << rangeStart << " .. " << rangeEnd << std::endl;
  365. }
  366. // Format for a location is line:col@offset
  367. VERIFY_ARE_EQUAL_WSTR(
  368. L"main(uint3) - spelling 2:6@28 - extent 2:1@23 .. 4:2@74\n",
  369. offsetStream.str().c_str());
  370. }
  371. }
  372. TEST_F(DXIntellisenseTest, QualifiedNameClass) {
  373. char program[] =
  374. "class TheClass {\r\n"
  375. "};\r\n"
  376. "TheClass C;";
  377. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  378. ExpectQualifiedName(result.TU, 3, 1, L"TheClass");
  379. }
  380. TEST_F(DXIntellisenseTest, CursorWhenGlobalVariableThenSimpleNames) {
  381. char program[] =
  382. "namespace Ns { class TheClass {\r\n"
  383. "}; }\r\n"
  384. "Ns::TheClass C;";
  385. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  386. // Qualified name does not include type.
  387. ExpectQualifiedName(result.TU, 3, 14, L"C");
  388. // Decalaration name includes type.
  389. ExpectDeclarationText(result.TU, 3, 14, L"Ns::TheClass C");
  390. // Semicolon is empty.
  391. ExpectQualifiedName(result.TU, 3, 15, L"");
  392. }
  393. TEST_F(DXIntellisenseTest, CursorWhenTypeOfVariableDeclThenNamesHaveType) {
  394. char program[] =
  395. "namespace Ns { class TheClass {\r\n"
  396. "}; }\r\n"
  397. "Ns::TheClass C;";
  398. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  399. ExpectQualifiedName(result.TU, 3, 1, L"Ns");
  400. ExpectDeclarationText(result.TU, 3, 1, L"namespace Ns");
  401. ExpectQualifiedName(result.TU, 3, 6, L"Ns::TheClass");
  402. ExpectDeclarationText(result.TU, 3, 6, L"class Ns::TheClass");
  403. }
  404. TEST_F(DXIntellisenseTest, CursorWhenVariableRefThenSimpleNames) {
  405. char program[] =
  406. "namespace Ns { class TheClass {\r\n"
  407. "public: int f;\r\n"
  408. "}; }\r\n"
  409. "void fn() {\r\n"
  410. "Ns::TheClass C;\r\n"
  411. "C.f = 1;\r\n"
  412. "}";
  413. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  414. ExpectCursorAt(result.TU, 6, 1, DxcCursor_DeclRefExpr);
  415. ExpectQualifiedName(result.TU, 6, 1, L"C");
  416. }
  417. TEST_F(DXIntellisenseTest, CursorWhenFieldRefThenSimpleNames) {
  418. char program[] =
  419. "namespace Ns { class TheClass {\r\n"
  420. "public: int f;\r\n"
  421. "}; }\r\n"
  422. "void fn() {\r\n"
  423. "Ns::TheClass C;\r\n"
  424. "C.f = 1;\r\n"
  425. "}";
  426. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  427. ExpectQualifiedName(result.TU, 6, 3, L"int f");
  428. }
  429. TEST_F(DXIntellisenseTest, QualifiedNameVariable) {
  430. char program[] =
  431. "namespace Ns { class TheClass {\r\n"
  432. "}; }\r\n"
  433. "Ns::TheClass C;";
  434. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  435. ExpectQualifiedName(result.TU, 3, 14, L"C");
  436. }
  437. TEST_F(DXIntellisenseTest, CursorWhenOverloadedResolvedThenDirectSymbol) {
  438. char program[] =
  439. "int abc(int);\r\n"
  440. "float abc(float);\r\n"
  441. "void foo() {\r\n"
  442. "int i = abc(123);\r\n"
  443. "}\r\n";
  444. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  445. CComPtr<IDxcCursor> cursor;
  446. GetCursorAt(result.TU, 4, 10, &cursor);
  447. DxcCursorKind kind;
  448. // 'abc' in 'abc(123)' is an expression that refers to a declaration.
  449. ASSERT_HRESULT_SUCCEEDED(cursor->GetKind(&kind));
  450. EXPECT_EQ(DxcCursor_DeclRefExpr, kind);
  451. // The referenced declaration is a function declaration.
  452. CComPtr<IDxcCursor> referenced;
  453. ASSERT_HRESULT_SUCCEEDED(cursor->GetReferencedCursor(&referenced));
  454. ASSERT_HRESULT_SUCCEEDED(referenced->GetKind(&kind));
  455. EXPECT_EQ(DxcCursor_FunctionDecl, kind);
  456. }
  457. TEST_F(DXIntellisenseTest, CursorWhenOverloadedIncompleteThenInvisible) {
  458. char program[] =
  459. "int abc(int);\r\n"
  460. "float abc(float);\r\n"
  461. "void foo() {\r\n"
  462. "int i = abc();\r\n"
  463. "}";
  464. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  465. CComPtr<IDxcCursor> cursor;
  466. GetCursorAt(result.TU, 4, 10, &cursor);
  467. DxcCursorKind kind;
  468. // 'abc' in 'abc()' is just part of the declaration - it's not a valid standalone entity
  469. ASSERT_HRESULT_SUCCEEDED(cursor->GetKind(&kind));
  470. EXPECT_EQ(DxcCursor_DeclStmt, kind);
  471. // The child of the declaration statement is the declaration.
  472. CComPtr<IDxcCursor> decl;
  473. ASSERT_HRESULT_SUCCEEDED(GetFirstChildFromCursor(cursor, &decl));
  474. ASSERT_HRESULT_SUCCEEDED(decl->GetKind(&kind));
  475. EXPECT_EQ(DxcCursor_VarDecl, kind);
  476. }
  477. TEST_F(DXIntellisenseTest, CursorWhenVariableUsedThenDeclarationAvailable) {
  478. char program[] =
  479. "int foo() {\r\n"
  480. "int i = 1;\r\n"
  481. "return i;\r\n"
  482. "}";
  483. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  484. CComPtr<IDxcCursor> cursor;
  485. // 'abc' in 'abc()' is just part of the declaration - it's not a valid standalone entity
  486. ExpectCursorAt(result.TU, 3, 8, DxcCursor_DeclRefExpr, &cursor);
  487. // The referenced declaration is a variable declaration.
  488. CComPtr<IDxcCursor> referenced;
  489. DxcCursorKind kind;
  490. ASSERT_HRESULT_SUCCEEDED(cursor->GetReferencedCursor(&referenced));
  491. ASSERT_HRESULT_SUCCEEDED(referenced->GetKind(&kind));
  492. EXPECT_EQ(DxcCursor_VarDecl, kind);
  493. CComBSTR name;
  494. ASSERT_HRESULT_SUCCEEDED(referenced->GetFormattedName(DxcCursorFormatting_Default, &name));
  495. EXPECT_STREQW(L"i", name);
  496. }
  497. // TODO: get a referenced local variable as 'int localVar';
  498. // TODO: get a referenced type as 'class Something';
  499. // TODO: having the caret on a function name in definition, highlights calls to it
  500. // TODO: get a class name without the template arguments
  501. // TODO: get a class name with ftemplate arguments
  502. // TODO: code completion for a built-in function
  503. // TODO: code completion for a built-in method
  504. void DXIntellisenseTest::CursorWhenFunctionThenSignatureAvailable()
  505. {
  506. char program[] =
  507. "int myfunc(int a, float b) { return a + b; }\r\n"
  508. "int foo() {\r\n"
  509. "myfunc(1, 2.0f);\r\n"
  510. "}";
  511. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  512. CComPtr<IDxcCursor> cursor;
  513. ExpectCursorAt(result.TU, 3, 1, DxcCursor_DeclRefExpr, &cursor);
  514. // TODO - how to get signature?
  515. }
  516. void DXIntellisenseTest::CursorWhenFunctionThenParamsAvailable()
  517. {
  518. char program[] =
  519. "int myfunc(int a, float b) { return a + b; }\r\n"
  520. "int foo() {\r\n"
  521. "myfunc(1, 2.0f);\r\n"
  522. "}";
  523. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  524. CComPtr<IDxcCursor> cursor;
  525. ExpectCursorAt(result.TU, 3, 1, DxcCursor_DeclRefExpr, &cursor);
  526. int argCount;
  527. VERIFY_SUCCEEDED(cursor->GetNumArguments(&argCount));
  528. VERIFY_ARE_EQUAL(-1, argCount); // The reference doesn't have the argument count - we need to resolve to func decl
  529. // TODO - how to get signature?
  530. }
  531. void DXIntellisenseTest::CursorWhenFunctionThenReturnTypeAvailable()
  532. {
  533. char program[] =
  534. "int myfunc(int a, float b) { return a + b; }\r\n"
  535. "int foo() {\r\n"
  536. "myfunc(1, 2.0f);\r\n"
  537. "}";
  538. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  539. CComPtr<IDxcCursor> cursor;
  540. ExpectCursorAt(result.TU, 3, 1, DxcCursor_DeclRefExpr, &cursor);
  541. // TODO - how to get signature?
  542. }
  543. void DXIntellisenseTest::CursorWhenReferenceThenDefinitionAvailable()
  544. {
  545. char program[] =
  546. "int myfunc(int a, float b) { return a + b; }\r\n"
  547. "int foo() {\r\n"
  548. "myfunc(1, 2.0f);\r\n"
  549. "}";
  550. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  551. CComPtr<IDxcCursor> cursor;
  552. CComPtr<IDxcCursor> defCursor;
  553. CComPtr<IDxcSourceLocation> defLocation;
  554. CComPtr<IDxcFile> defFile;
  555. unsigned line, col, offset;
  556. ExpectCursorAt(result.TU, 3, 1, DxcCursor_DeclRefExpr, &cursor);
  557. VERIFY_SUCCEEDED(cursor->GetDefinitionCursor(&defCursor));
  558. VERIFY_IS_NOT_NULL(defCursor.p);
  559. DxcCursorKind kind;
  560. VERIFY_SUCCEEDED(defCursor->GetKind(&kind));
  561. VERIFY_ARE_EQUAL(DxcCursor_FunctionDecl, kind);
  562. VERIFY_SUCCEEDED(defCursor->GetLocation(&defLocation));
  563. VERIFY_SUCCEEDED(defLocation->GetSpellingLocation(&defFile, &line, &col, &offset));
  564. VERIFY_ARE_EQUAL(1, line);
  565. VERIFY_ARE_EQUAL(5, col); // Points to 'myfunc'
  566. VERIFY_ARE_EQUAL(4, offset); // Offset is zero-based
  567. }
  568. void DXIntellisenseTest::CursorWhenFindAtBodyCallThenMatch()
  569. {
  570. char program[] =
  571. "int f();\r\n"
  572. "int main() {\r\n"
  573. " f(); }";
  574. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  575. CComPtr<IDxcCursor> cursor;
  576. ExpectCursorAt(result.TU, 3, 3, DxcCursor_DeclRefExpr, &cursor);
  577. }
  578. void DXIntellisenseTest::CursorWhenFindAtGlobalThenMatch()
  579. {
  580. char program[] = "int a;";
  581. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  582. CComPtr<IDxcCursor> cursor;
  583. ExpectCursorAt(result.TU, 1, 4, DxcCursor_VarDecl, &cursor);
  584. }
  585. void DXIntellisenseTest::CursorWhenFindBeforeBodyCallThenMatch()
  586. {
  587. char program[] =
  588. "int f();\r\n"
  589. "int main() {\r\n"
  590. " f(); }";
  591. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  592. CComPtr<IDxcCursor> cursor;
  593. ExpectCursorAt(result.TU, 3, 1, DxcCursor_CompoundStmt, &cursor);
  594. CComPtr<IDxcCursor> snappedCursor;
  595. CComPtr<IDxcSourceLocation> location;
  596. DxcCursorKind cursorKind;
  597. GetLocationAt(result.TU, 3, 1, &location);
  598. VERIFY_SUCCEEDED(cursor->GetSnappedChild(location, &snappedCursor));
  599. VERIFY_IS_NOT_NULL(snappedCursor.p);
  600. VERIFY_SUCCEEDED(snappedCursor->GetKind(&cursorKind));
  601. VERIFY_ARE_EQUAL(DxcCursor_DeclRefExpr, cursorKind);
  602. }
  603. void DXIntellisenseTest::CursorWhenFindBeforeGlobalThenMatch()
  604. {
  605. char program[] = " int a;";
  606. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  607. CComPtr<IDxcCursor> cursor;
  608. ExpectCursorAt(result.TU, 1, 1, DxcCursor_NoDeclFound, &cursor);
  609. cursor.Release();
  610. CComPtr<IDxcCursor> snappedCursor;
  611. CComPtr<IDxcSourceLocation> location;
  612. DxcCursorKind cursorKind;
  613. GetLocationAt(result.TU, 1, 1, &location);
  614. VERIFY_SUCCEEDED(result.TU->GetCursor(&cursor));
  615. VERIFY_SUCCEEDED(cursor->GetSnappedChild(location, &snappedCursor));
  616. VERIFY_IS_NOT_NULL(snappedCursor.p);
  617. VERIFY_SUCCEEDED(snappedCursor->GetKind(&cursorKind));
  618. VERIFY_ARE_EQUAL(DxcCursor_VarDecl, cursorKind);
  619. }
  620. void DXIntellisenseTest::FileWhenSameThenEqual()
  621. {
  622. char program[] = "int a;\r\nint b;";
  623. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  624. CComPtr<IDxcSourceLocation> location0, location1;
  625. CComPtr<IDxcFile> file0, file1;
  626. unsigned line, col, offset;
  627. BOOL isEqual;
  628. GetLocationAt(result.TU, 1, 1, &location0);
  629. GetLocationAt(result.TU, 2, 1, &location1);
  630. VERIFY_SUCCEEDED(location0->GetSpellingLocation(&file0, &line, &col, &offset));
  631. VERIFY_SUCCEEDED(location1->GetSpellingLocation(&file1, &line, &col, &offset));
  632. VERIFY_SUCCEEDED(file0->IsEqualTo(file1, &isEqual));
  633. VERIFY_ARE_EQUAL(TRUE, isEqual);
  634. }
  635. void DXIntellisenseTest::FileWhenNotSameThenNotEqual()
  636. {
  637. char program[] = "int a;\r\nint b;";
  638. CompilationResult result0(CompilationResult::CreateForProgram(program, _countof(program)));
  639. CompilationResult result1(CompilationResult::CreateForProgram(program, _countof(program)));
  640. CComPtr<IDxcSourceLocation> location0, location1;
  641. CComPtr<IDxcFile> file0, file1;
  642. unsigned line, col, offset;
  643. BOOL isEqual;
  644. GetLocationAt(result0.TU, 1, 1, &location0);
  645. GetLocationAt(result1.TU, 1, 1, &location1);
  646. VERIFY_SUCCEEDED(location0->GetSpellingLocation(&file0, &line, &col, &offset));
  647. VERIFY_SUCCEEDED(location1->GetSpellingLocation(&file1, &line, &col, &offset));
  648. VERIFY_SUCCEEDED(file0->IsEqualTo(file1, &isEqual));
  649. VERIFY_ARE_EQUAL(FALSE, isEqual);
  650. }
  651. void DXIntellisenseTest::TypeWhenICEThenEval()
  652. {
  653. // When an ICE is present in a declaration, it appears in the name.
  654. char program[] =
  655. "float c[(1, 2)];\r\n"
  656. "float main() : SV_Target\r\n"
  657. "{ return c[0]; }";
  658. CompilationResult result(CompilationResult::CreateForProgram(program, _countof(program)));
  659. VERIFY_ARE_EQUAL(true, result.ParseSucceeded());
  660. CComPtr<IDxcCursor> cCursor;
  661. ExpectCursorAt(result.TU, 1, 7, DxcCursor_VarDecl, &cCursor);
  662. CComPtr<IDxcType> typeCursor;
  663. VERIFY_SUCCEEDED(cCursor->GetCursorType(&typeCursor));
  664. CComHeapPtr<char> name;
  665. VERIFY_SUCCEEDED(typeCursor->GetSpelling(&name));
  666. VERIFY_ARE_EQUAL_STR("const float [2]", name); // global variables converted to const by default
  667. }