Pārlūkot izejas kodu

dxcisense: Allocate "TM" classes using IMalloc instead of new (#3258)

When running valgrind over a program using `DxcIntelliSense` (to
validate our own deallocations [1]) a bunch of mismatching `new` with
`free()` show up:

    Mismatched free() / delete / delete []
       at 0x483B9AB: free (vg_replace_malloc.c:538)
       by 0x52B06A8: IMalloc::Free(void*) (WinAdapter.cpp:34)
       by 0x6DC7D19: DxcTranslationUnit::Release() (dxcisenseimpl.h:308)
       by 0x14278E: com_rs::unknown::IUnknown::release (unknown.rs:55)
       [...]
     Address 0x4c3c930 is 0 bytes inside a block of size 40 alloc'd
       at 0x483B07F: operator new(unsigned long, std::nothrow_t const&) (vg_replace_malloc.c:385)
       by 0x6DC06EB: DxcIndex::ParseTranslationUnit(char const*, char const* const*, int, IDxcUnsavedFile**, unsigned int, DxcTranslationUnitFlags, IDxcTranslationUnit**) (dxcisenseimpl.cpp:1192)
       by 0x13020A: hassle_rs::intellisense::ffi::IDxcIndex::parse_translation_unit (macros.rs:108)
       by 0x119B74: hassle_rs::intellisense::wrapper::DxcIndex::parse_translation_unit (wrapper.rs:101)
       [...]

And so on for the other intellisense classes.  All these classes have
`DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL` which deallocates `this` on the
associated `m_pMalloc` with `free()`:

    The "TM" version keep an IMalloc field that, if not null, indicate
    ownership of 'this' and of any allocations used during release.

Yet are allocated using `new`, resulting in this mismatch.  The solution
is to follow a similar approach as the introduction of `IMalloc` to
`DxcIntelliSense` in d5bb308 by rewriting all classes to take an
`IMalloc *` in the constructor and invoking it either through `::Alloc`
from `DXC_MICROCOM_TM_ALLOC` or `CreateOnMalloc`.

[1]: https://github.com/microsoft/DirectXShaderCompiler/pull/3250#issuecomment-726216850
Marijn Suijten 4 gadi atpakaļ
vecāks
revīzija
5f835c4506

+ 38 - 115
tools/clang/tools/libclang/dxcisenseimpl.cpp

@@ -60,12 +60,13 @@ private:
   unsigned m_length;
   unsigned m_length;
 public:
 public:
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
+  DXC_MICROCOM_TM_ALLOC(DxcBasicUnsavedFile)
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   {
   {
     return DoBasicQueryInterface<IDxcUnsavedFile>(this, iid, ppvObject);
     return DoBasicQueryInterface<IDxcUnsavedFile>(this, iid, ppvObject);
   }
   }
 
 
-  DxcBasicUnsavedFile();
+  DxcBasicUnsavedFile(IMalloc *pMalloc);
   ~DxcBasicUnsavedFile();
   ~DxcBasicUnsavedFile();
   HRESULT Initialize(_In_z_ LPCSTR fileName, _In_z_ LPCSTR contents, unsigned length);
   HRESULT Initialize(_In_z_ LPCSTR fileName, _In_z_ LPCSTR contents, unsigned length);
   static HRESULT Create(_In_z_ LPCSTR fileName, _In_z_ LPCSTR contents, unsigned length, _COM_Outptr_ IDxcUnsavedFile** pObject);
   static HRESULT Create(_In_z_ LPCSTR fileName, _In_z_ LPCSTR contents, unsigned length, _COM_Outptr_ IDxcUnsavedFile** pObject);
@@ -496,16 +497,16 @@ CXChildVisitResult LIBCLANG_CC SourceCursorVisit(CXCursor cursor, CXCursor paren
     return CXChildVisit_Continue;
     return CXChildVisit_Continue;
   }
   }
 
 
-  // If the range start is before or equal to our position, we cover the 
-  // location, and we have a result but might need to recurse. If the range 
-  // start is after, this is where snapping behavior kicks in, and we'll 
-  // consider it just as good as overlap (for the closest match we have). So we 
+  // If the range start is before or equal to our position, we cover the
+  // location, and we have a result but might need to recurse. If the range
+  // start is after, this is where snapping behavior kicks in, and we'll
+  // consider it just as good as overlap (for the closest match we have). So we
   // don't in fact need to consider the value, other than to snap to the closest
   // don't in fact need to consider the value, other than to snap to the closest
   // cursor.
   // cursor.
   unsigned cursorStartOffset;
   unsigned cursorStartOffset;
   clang_getSpellingLocation(clang_getRangeStart(range), &cursorFile, nullptr, nullptr, &cursorStartOffset);
   clang_getSpellingLocation(clang_getRangeStart(range), &cursorFile, nullptr, nullptr, &cursorStartOffset);
 
 
-  bool isKindResult = 
+  bool isKindResult =
     cursor.kind != CXCursor_CompoundStmt &&
     cursor.kind != CXCursor_CompoundStmt &&
     cursor.kind != CXCursor_TranslationUnit &&
     cursor.kind != CXCursor_TranslationUnit &&
     !clang_isInvalid(cursor.kind);
     !clang_isInvalid(cursor.kind);
@@ -528,9 +529,10 @@ CXChildVisitResult LIBCLANG_CC SourceCursorVisit(CXCursor cursor, CXCursor paren
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcBasicUnsavedFile::DxcBasicUnsavedFile() : m_fileName(nullptr), m_contents(nullptr)
+DxcBasicUnsavedFile::DxcBasicUnsavedFile(IMalloc *pMalloc)
+    : m_dwRef(0), m_pMalloc(pMalloc)
+    , m_fileName(nullptr), m_contents(nullptr)
 {
 {
-  m_pMalloc = DxcGetThreadMallocNoRef();
 }
 }
 
 
 DxcBasicUnsavedFile::~DxcBasicUnsavedFile()
 DxcBasicUnsavedFile::~DxcBasicUnsavedFile()
@@ -574,7 +576,7 @@ HRESULT DxcBasicUnsavedFile::Create(
 {
 {
   if (pObject == nullptr) return E_POINTER;
   if (pObject == nullptr) return E_POINTER;
   *pObject = nullptr;
   *pObject = nullptr;
-  DxcBasicUnsavedFile* newValue = new (std::nothrow)DxcBasicUnsavedFile();
+  DxcBasicUnsavedFile* newValue = DxcBasicUnsavedFile::Alloc(DxcGetThreadMallocNoRef());
   if (newValue == nullptr) return E_OUTOFMEMORY;
   if (newValue == nullptr) return E_OUTOFMEMORY;
   HRESULT hr = newValue->Initialize(fileName, contents, contentLength);
   HRESULT hr = newValue->Initialize(fileName, contents, contentLength);
   if (FAILED(hr))
   if (FAILED(hr))
@@ -608,21 +610,12 @@ HRESULT DxcBasicUnsavedFile::GetLength(unsigned* pLength)
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcCursor::DxcCursor()
-{
-  m_pMalloc = DxcGetThreadMallocNoRef();
-}
-
-DxcCursor::~DxcCursor()
-{
-}
-
 _Use_decl_annotations_
 _Use_decl_annotations_
 HRESULT DxcCursor::Create(const CXCursor& cursor, IDxcCursor** pObject)
 HRESULT DxcCursor::Create(const CXCursor& cursor, IDxcCursor** pObject)
 {
 {
   if (pObject == nullptr) return E_POINTER;
   if (pObject == nullptr) return E_POINTER;
   *pObject = nullptr;
   *pObject = nullptr;
-  DxcCursor* newValue = new (std::nothrow)DxcCursor();
+  DxcCursor* newValue = DxcCursor::Alloc(DxcGetThreadMallocNoRef());
   if (newValue == nullptr) return E_OUTOFMEMORY;
   if (newValue == nullptr) return E_OUTOFMEMORY;
   newValue->Initialize(cursor);
   newValue->Initialize(cursor);
   newValue->AddRef();
   newValue->AddRef();
@@ -864,9 +857,10 @@ HRESULT DxcCursor::GetSnappedChild(IDxcSourceLocation* location, IDxcCursor** pR
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcDiagnostic::DxcDiagnostic() : m_diagnostic(nullptr)
+DxcDiagnostic::DxcDiagnostic(IMalloc *pMalloc)
+    : m_dwRef(0), m_pMalloc(pMalloc)
+    , m_diagnostic(nullptr)
 {
 {
-  m_pMalloc = DxcGetThreadMallocNoRef();
 }
 }
 
 
 DxcDiagnostic::~DxcDiagnostic()
 DxcDiagnostic::~DxcDiagnostic()
@@ -887,7 +881,7 @@ HRESULT DxcDiagnostic::Create(const CXDiagnostic& diagnostic, IDxcDiagnostic** p
 {
 {
   if (pObject == nullptr) return E_POINTER;
   if (pObject == nullptr) return E_POINTER;
   *pObject = nullptr;
   *pObject = nullptr;
-  DxcDiagnostic* newValue = new (std::nothrow) DxcDiagnostic();
+  DxcDiagnostic* newValue = DxcDiagnostic::Alloc(DxcGetThreadMallocNoRef());
   if (newValue == nullptr) return E_OUTOFMEMORY;
   if (newValue == nullptr) return E_OUTOFMEMORY;
   newValue->Initialize(diagnostic);
   newValue->Initialize(diagnostic);
   newValue->AddRef();
   newValue->AddRef();
@@ -978,15 +972,6 @@ HRESULT DxcDiagnostic::GetFixItAt(unsigned index,
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcFile::DxcFile()
-{
-  m_pMalloc = DxcGetThreadMallocNoRef();
-}
-
-DxcFile::~DxcFile()
-{
-}
-
 void DxcFile::Initialize(const CXFile& file)
 void DxcFile::Initialize(const CXFile& file)
 {
 {
   m_file = file;
   m_file = file;
@@ -997,7 +982,7 @@ HRESULT DxcFile::Create(const CXFile& file, IDxcFile** pObject)
 {
 {
   if (pObject == nullptr) return E_POINTER;
   if (pObject == nullptr) return E_POINTER;
   *pObject = nullptr;
   *pObject = nullptr;
-  DxcFile* newValue = new (std::nothrow)DxcFile();
+  DxcFile* newValue = DxcFile::Alloc(DxcGetThreadMallocNoRef());
   if (newValue == nullptr) return E_OUTOFMEMORY;
   if (newValue == nullptr) return E_OUTOFMEMORY;
   newValue->Initialize(file);
   newValue->Initialize(file);
   newValue->AddRef();
   newValue->AddRef();
@@ -1032,9 +1017,9 @@ HRESULT DxcFile::IsEqualTo(IDxcFile* other, BOOL* pResult)
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcInclusion::DxcInclusion()
-    : m_file(nullptr), m_locations(nullptr), m_locationLength(0) {
-  m_pMalloc = DxcGetThreadMallocNoRef();
+DxcInclusion::DxcInclusion(IMalloc *pMalloc)
+    : m_dwRef(0), m_pMalloc(pMalloc)
+    , m_file(nullptr), m_locations(nullptr), m_locationLength(0) {
 }
 }
 
 
 DxcInclusion::~DxcInclusion() {
 DxcInclusion::~DxcInclusion() {
@@ -1060,7 +1045,7 @@ HRESULT DxcInclusion::Create(CXFile file, unsigned locations, CXSourceLocation *
   *pResult = nullptr;
   *pResult = nullptr;
 
 
   CComPtr<DxcInclusion> local;
   CComPtr<DxcInclusion> local;
-  local = new (std::nothrow)DxcInclusion();
+  local = DxcInclusion::Alloc(DxcGetThreadMallocNoRef());
   if (local == nullptr) return E_OUTOFMEMORY;
   if (local == nullptr) return E_OUTOFMEMORY;
   HRESULT hr = local->Initialize(file, locations, pLocations);
   HRESULT hr = local->Initialize(file, locations, pLocations);
   if (FAILED(hr)) return hr;
   if (FAILED(hr)) return hr;
@@ -1091,9 +1076,10 @@ HRESULT DxcInclusion::GetStackItem(unsigned index, _Outptr_result_nullonfailure_
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcIndex::DxcIndex() : m_index(0), m_options(DxcGlobalOpt_None)
+DxcIndex::DxcIndex(IMalloc *pMalloc)
+    : m_dwRef(0), m_pMalloc(pMalloc)
+    , m_index(0), m_options(DxcGlobalOpt_None)
 {
 {
-  m_pMalloc = DxcGetThreadMallocNoRef();
 }
 }
 
 
 DxcIndex::~DxcIndex()
 DxcIndex::~DxcIndex()
@@ -1127,7 +1113,7 @@ HRESULT DxcIndex::Create(hlsl::DxcLangExtensionsHelper &langHelper, DxcIndex** i
   *index = nullptr;
   *index = nullptr;
 
 
   CComPtr<DxcIndex> local;
   CComPtr<DxcIndex> local;
-  local = new (std::nothrow) DxcIndex();
+  local = DxcIndex::Alloc(DxcGetThreadMallocNoRef());
   if (local == nullptr) return E_OUTOFMEMORY;
   if (local == nullptr) return E_OUTOFMEMORY;
   HRESULT hr = local->Initialize(langHelper);
   HRESULT hr = local->Initialize(langHelper);
   if (FAILED(hr)) return hr;
   if (FAILED(hr)) return hr;
@@ -1189,7 +1175,7 @@ HRESULT DxcIndex::ParseTranslationUnit(
       return E_FAIL;
       return E_FAIL;
     }
     }
 
 
-    CComPtr<DxcTranslationUnit> localTU = new (std::nothrow) DxcTranslationUnit();
+    CComPtr<DxcTranslationUnit> localTU = DxcTranslationUnit::Alloc(DxcGetThreadMallocNoRef());
     if (localTU == nullptr)
     if (localTU == nullptr)
     {
     {
       clang_disposeTranslationUnit(tu);
       clang_disposeTranslationUnit(tu);
@@ -1205,11 +1191,6 @@ HRESULT DxcIndex::ParseTranslationUnit(
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcIntelliSense::DxcIntelliSense(IMalloc *pMalloc)
-{
-  m_pMalloc = pMalloc;
-}
-
 _Use_decl_annotations_
 _Use_decl_annotations_
 HRESULT DxcIntelliSense::CreateIndex(IDxcIndex** index)
 HRESULT DxcIntelliSense::CreateIndex(IDxcIndex** index)
 {
 {
@@ -1275,15 +1256,6 @@ HRESULT DxcIntelliSense::CreateUnsavedFile(LPCSTR fileName, LPCSTR contents, uns
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcSourceLocation::DxcSourceLocation()
-{
-  m_pMalloc = DxcGetThreadMallocNoRef();
-}
-
-DxcSourceLocation::~DxcSourceLocation()
-{
-}
-
 void DxcSourceLocation::Initialize(const CXSourceLocation& location)
 void DxcSourceLocation::Initialize(const CXSourceLocation& location)
 {
 {
   m_location = location;
   m_location = location;
@@ -1296,7 +1268,7 @@ HRESULT DxcSourceLocation::Create(
 {
 {
   if (pObject == nullptr) return E_POINTER;
   if (pObject == nullptr) return E_POINTER;
   *pObject = nullptr;
   *pObject = nullptr;
-  DxcSourceLocation* local = new (std::nothrow)DxcSourceLocation();
+  DxcSourceLocation* local = DxcSourceLocation::Alloc(DxcGetThreadMallocNoRef());
   if (local == nullptr) return E_OUTOFMEMORY;
   if (local == nullptr) return E_OUTOFMEMORY;
   local->Initialize(location);
   local->Initialize(location);
   local->AddRef();
   local->AddRef();
@@ -1355,7 +1327,7 @@ HRESULT DxcSourceLocation::GetPresumedLocation(
   _Out_opt_ unsigned* pCol)
   _Out_opt_ unsigned* pCol)
 {
 {
   DxcThreadMalloc TM(m_pMalloc);
   DxcThreadMalloc TM(m_pMalloc);
-  
+
   CXString filename;
   CXString filename;
   unsigned line, col;
   unsigned line, col;
   clang_getPresumedLocation(m_location, &filename, &line, &col);
   clang_getPresumedLocation(m_location, &filename, &line, &col);
@@ -1371,15 +1343,6 @@ HRESULT DxcSourceLocation::GetPresumedLocation(
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcSourceRange::DxcSourceRange()
-{
-  m_pMalloc = DxcGetThreadMallocNoRef();
-}
-
-DxcSourceRange::~DxcSourceRange()
-{
-}
-
 void DxcSourceRange::Initialize(const CXSourceRange& range)
 void DxcSourceRange::Initialize(const CXSourceRange& range)
 {
 {
   m_range = range;
   m_range = range;
@@ -1390,7 +1353,7 @@ HRESULT DxcSourceRange::Create(const CXSourceRange& range, IDxcSourceRange** pOb
 {
 {
   if (pObject == nullptr) return E_POINTER;
   if (pObject == nullptr) return E_POINTER;
   *pObject = nullptr;
   *pObject = nullptr;
-  DxcSourceRange* local = new (std::nothrow)DxcSourceRange();
+  DxcSourceRange* local = DxcSourceRange::Alloc(DxcGetThreadMallocNoRef());
   if (local == nullptr) return E_OUTOFMEMORY;
   if (local == nullptr) return E_OUTOFMEMORY;
   local->Initialize(range);
   local->Initialize(range);
   local->AddRef();
   local->AddRef();
@@ -1439,15 +1402,6 @@ HRESULT DxcSourceRange::GetOffsets(_Out_ unsigned* startOffset, _Out_ unsigned*
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcToken::DxcToken()
-{
-  m_pMalloc = DxcGetThreadMallocNoRef();
-}
-
-DxcToken::~DxcToken()
-{
-}
-
 void DxcToken::Initialize(const CXTranslationUnit& tu, const CXToken& token)
 void DxcToken::Initialize(const CXTranslationUnit& tu, const CXToken& token)
 {
 {
   m_tu = tu;
   m_tu = tu;
@@ -1462,7 +1416,7 @@ HRESULT DxcToken::Create(
 {
 {
   if (pObject == nullptr) return E_POINTER;
   if (pObject == nullptr) return E_POINTER;
   *pObject = nullptr;
   *pObject = nullptr;
-  DxcToken* local = new (std::nothrow)DxcToken();
+  DxcToken* local = DxcToken::Alloc(DxcGetThreadMallocNoRef());
   if (local == nullptr) return E_OUTOFMEMORY;
   if (local == nullptr) return E_OUTOFMEMORY;
   local->Initialize(tu, token);
   local->Initialize(tu, token);
   local->AddRef();
   local->AddRef();
@@ -1512,9 +1466,10 @@ HRESULT DxcToken::GetSpelling(LPSTR* pValue)
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcTranslationUnit::DxcTranslationUnit() : m_tu(nullptr)
+DxcTranslationUnit::DxcTranslationUnit(IMalloc *pMalloc)
+    : m_dwRef(0), m_pMalloc(pMalloc)
+    , m_tu(nullptr)
 {
 {
-  m_pMalloc = DxcGetThreadMallocNoRef();
 }
 }
 
 
 DxcTranslationUnit::~DxcTranslationUnit() {
 DxcTranslationUnit::~DxcTranslationUnit() {
@@ -1866,7 +1821,7 @@ HRESULT DxcTranslationUnit::CodeCompleteAt(
   if (results == nullptr) return E_FAIL;
   if (results == nullptr) return E_FAIL;
   *pResult = nullptr;
   *pResult = nullptr;
   DxcCodeCompleteResults *newValue =
   DxcCodeCompleteResults *newValue =
-      new (std::nothrow) DxcCodeCompleteResults();
+      DxcCodeCompleteResults::Alloc(DxcGetThreadMallocNoRef());
   if (newValue == nullptr)
   if (newValue == nullptr)
   {
   {
 	  clang_disposeCodeCompleteResults(results);
 	  clang_disposeCodeCompleteResults(results);
@@ -1880,21 +1835,12 @@ HRESULT DxcTranslationUnit::CodeCompleteAt(
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcType::DxcType()
-{
-  m_pMalloc = DxcGetThreadMallocNoRef();
-}
-
-DxcType::~DxcType()
-{
-}
-
 _Use_decl_annotations_
 _Use_decl_annotations_
 HRESULT DxcType::Create(const CXType& type, IDxcType** pObject)
 HRESULT DxcType::Create(const CXType& type, IDxcType** pObject)
 {
 {
   if (pObject == nullptr) return E_POINTER;
   if (pObject == nullptr) return E_POINTER;
   *pObject = nullptr;
   *pObject = nullptr;
-  DxcType* newValue = new (std::nothrow) DxcType();
+  DxcType* newValue = DxcType::Alloc(DxcGetThreadMallocNoRef());
   if (newValue == nullptr) return E_OUTOFMEMORY;
   if (newValue == nullptr) return E_OUTOFMEMORY;
   newValue->Initialize(type);
   newValue->Initialize(type);
   newValue->AddRef();
   newValue->AddRef();
@@ -1938,11 +1884,6 @@ HRESULT DxcType::GetKind(DxcTypeKind* pResult)
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcCodeCompleteResults::DxcCodeCompleteResults()
-{
-  m_pMalloc = DxcGetThreadMallocNoRef();
-}
-
 DxcCodeCompleteResults::~DxcCodeCompleteResults()
 DxcCodeCompleteResults::~DxcCodeCompleteResults()
 {
 {
 	clang_disposeCodeCompleteResults(m_ccr);
 	clang_disposeCodeCompleteResults(m_ccr);
@@ -1978,7 +1919,7 @@ HRESULT DxcCodeCompleteResults::GetResultAt(
   CXCompletionResult result = m_ccr->Results[index];
   CXCompletionResult result = m_ccr->Results[index];
 
 
   *pResult = nullptr;
   *pResult = nullptr;
-  DxcCompletionResult *newValue = new (std::nothrow) DxcCompletionResult();
+  DxcCompletionResult *newValue = DxcCompletionResult::Alloc(DxcGetThreadMallocNoRef());
   if (newValue == nullptr)
   if (newValue == nullptr)
     return E_OUTOFMEMORY;
     return E_OUTOFMEMORY;
   newValue->Initialize(result);
   newValue->Initialize(result);
@@ -1990,15 +1931,6 @@ HRESULT DxcCodeCompleteResults::GetResultAt(
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcCompletionResult::DxcCompletionResult()
-{
-  m_pMalloc = DxcGetThreadMallocNoRef();
-}
-
-DxcCompletionResult::~DxcCompletionResult()
-{
-}
-
 void DxcCompletionResult::Initialize(const CXCompletionResult &cr)
 void DxcCompletionResult::Initialize(const CXCompletionResult &cr)
 {
 {
   m_cr = cr;
   m_cr = cr;
@@ -2020,7 +1952,7 @@ HRESULT DxcCompletionResult::GetCompletionString(IDxcCompletionString **pResult)
   DxcThreadMalloc TM(m_pMalloc);
   DxcThreadMalloc TM(m_pMalloc);
 
 
   *pResult = nullptr;
   *pResult = nullptr;
-  DxcCompletionString *newValue = new (std::nothrow) DxcCompletionString();
+  DxcCompletionString *newValue = DxcCompletionString::Alloc(DxcGetThreadMallocNoRef());
   if (newValue == nullptr)
   if (newValue == nullptr)
     return E_OUTOFMEMORY;
     return E_OUTOFMEMORY;
   newValue->Initialize(m_cr.CompletionString);
   newValue->Initialize(m_cr.CompletionString);
@@ -2032,15 +1964,6 @@ HRESULT DxcCompletionResult::GetCompletionString(IDxcCompletionString **pResult)
 
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
-DxcCompletionString::DxcCompletionString()
-{
-  m_pMalloc = DxcGetThreadMallocNoRef();
-}
-
-DxcCompletionString::~DxcCompletionString()
-{
-}
-
 void DxcCompletionString::Initialize(const CXCompletionString &cs)
 void DxcCompletionString::Initialize(const CXCompletionString &cs)
 {
 {
   m_cs = cs;
   m_cs = cs;
@@ -2264,4 +2187,4 @@ C_ASSERT((int)DxcCompletionChunk_Colon == (int)CXCompletionChunk_Colon);
 C_ASSERT((int)DxcCompletionChunk_SemiColon == (int)CXCompletionChunk_SemiColon);
 C_ASSERT((int)DxcCompletionChunk_SemiColon == (int)CXCompletionChunk_SemiColon);
 C_ASSERT((int)DxcCompletionChunk_Equal == (int)CXCompletionChunk_Equal);
 C_ASSERT((int)DxcCompletionChunk_Equal == (int)CXCompletionChunk_Equal);
 C_ASSERT((int)DxcCompletionChunk_HorizontalSpace == (int)CXCompletionChunk_HorizontalSpace);
 C_ASSERT((int)DxcCompletionChunk_HorizontalSpace == (int)CXCompletionChunk_HorizontalSpace);
-C_ASSERT((int)DxcCompletionChunk_VerticalSpace == (int)CXCompletionChunk_VerticalSpace);
+C_ASSERT((int)DxcCompletionChunk_VerticalSpace == (int)CXCompletionChunk_VerticalSpace);

+ 31 - 36
tools/clang/tools/libclang/dxcisenseimpl.h

@@ -37,13 +37,12 @@ private:
   CXCursor m_cursor;
   CXCursor m_cursor;
 public:
 public:
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
+  DXC_MICROCOM_TM_CTOR(DxcCursor)
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   {
   {
     return DoBasicQueryInterface<IDxcCursor>(this, iid, ppvObject);
     return DoBasicQueryInterface<IDxcCursor>(this, iid, ppvObject);
   }
   }
 
 
-  DxcCursor();
-  ~DxcCursor();
   void Initialize(const CXCursor& cursor);
   void Initialize(const CXCursor& cursor);
   static HRESULT Create(const CXCursor& cursor, _Outptr_result_nullonfailure_ IDxcCursor** pObject);
   static HRESULT Create(const CXCursor& cursor, _Outptr_result_nullonfailure_ IDxcCursor** pObject);
 
 
@@ -86,12 +85,13 @@ private:
   CXDiagnostic m_diagnostic;
   CXDiagnostic m_diagnostic;
 public:
 public:
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
+  DXC_MICROCOM_TM_ALLOC(DxcDiagnostic)
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   {
   {
     return DoBasicQueryInterface<IDxcDiagnostic>(this, iid, ppvObject);
     return DoBasicQueryInterface<IDxcDiagnostic>(this, iid, ppvObject);
   }
   }
 
 
-  DxcDiagnostic();
+  DxcDiagnostic(IMalloc *pMalloc);
   ~DxcDiagnostic();
   ~DxcDiagnostic();
   void Initialize(const CXDiagnostic& diagnostic);
   void Initialize(const CXDiagnostic& diagnostic);
   static HRESULT Create(const CXDiagnostic& diagnostic, _Outptr_result_nullonfailure_ IDxcDiagnostic** pObject);
   static HRESULT Create(const CXDiagnostic& diagnostic, _Outptr_result_nullonfailure_ IDxcDiagnostic** pObject);
@@ -117,13 +117,12 @@ private:
   CXFile m_file;
   CXFile m_file;
 public:
 public:
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
+  DXC_MICROCOM_TM_CTOR(DxcFile)
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   {
   {
     return DoBasicQueryInterface<IDxcFile>(this, iid, ppvObject);
     return DoBasicQueryInterface<IDxcFile>(this, iid, ppvObject);
   }
   }
 
 
-  DxcFile();
-  ~DxcFile();
   void Initialize(const CXFile& file);
   void Initialize(const CXFile& file);
   static HRESULT Create(const CXFile& file, _Outptr_result_nullonfailure_ IDxcFile** pObject);
   static HRESULT Create(const CXFile& file, _Outptr_result_nullonfailure_ IDxcFile** pObject);
 
 
@@ -142,12 +141,13 @@ private:
   unsigned m_locationLength;
   unsigned m_locationLength;
 public:
 public:
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
+  DXC_MICROCOM_TM_ALLOC(DxcInclusion)
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   {
   {
     return DoBasicQueryInterface<IDxcInclusion>(this, iid, ppvObject);
     return DoBasicQueryInterface<IDxcInclusion>(this, iid, ppvObject);
   }
   }
 
 
-  DxcInclusion();
+  DxcInclusion(IMalloc *pMalloc);
   ~DxcInclusion();
   ~DxcInclusion();
   HRESULT Initialize(CXFile file, unsigned locations, _In_count_(locations) CXSourceLocation *pLocation);
   HRESULT Initialize(CXFile file, unsigned locations, _In_count_(locations) CXSourceLocation *pLocation);
   static HRESULT Create(CXFile file, unsigned locations, _In_count_(locations) CXSourceLocation *pLocation, _COM_Outptr_ IDxcInclusion **pResult);
   static HRESULT Create(CXFile file, unsigned locations, _In_count_(locations) CXSourceLocation *pLocation, _COM_Outptr_ IDxcInclusion **pResult);
@@ -166,12 +166,13 @@ private:
     hlsl::DxcLangExtensionsHelper m_langHelper;
     hlsl::DxcLangExtensionsHelper m_langHelper;
 public:
 public:
     DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
     DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
+    DXC_MICROCOM_TM_ALLOC(DxcIndex)
     HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
     HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
     {
     {
       return DoBasicQueryInterface<IDxcIndex>(this, iid, ppvObject);
       return DoBasicQueryInterface<IDxcIndex>(this, iid, ppvObject);
     }
     }
 
 
-    DxcIndex();
+    DxcIndex(IMalloc *pMalloc);
     ~DxcIndex();
     ~DxcIndex();
     HRESULT Initialize(hlsl::DxcLangExtensionsHelper& langHelper);
     HRESULT Initialize(hlsl::DxcLangExtensionsHelper& langHelper);
     static HRESULT Create(hlsl::DxcLangExtensionsHelper& langHelper, _Outptr_result_nullonfailure_ DxcIndex** index);
     static HRESULT Create(hlsl::DxcLangExtensionsHelper& langHelper, _Outptr_result_nullonfailure_ DxcIndex** index);
@@ -195,6 +196,7 @@ private:
 
 
 public:
 public:
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL();
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL();
+  DXC_MICROCOM_TM_CTOR(DxcIntelliSense)
   DXC_LANGEXTENSIONS_HELPER_IMPL(m_langHelper);
   DXC_LANGEXTENSIONS_HELPER_IMPL(m_langHelper);
 
 
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject) override {
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject) override {
@@ -203,21 +205,19 @@ public:
         this, iid, ppvObject);
         this, iid, ppvObject);
   }
   }
 
 
-  DxcIntelliSense(IMalloc *pMalloc);
-
-    HRESULT STDMETHODCALLTYPE CreateIndex(_Outptr_result_nullonfailure_ IDxcIndex** index) override;
-    HRESULT STDMETHODCALLTYPE GetNullLocation(_Outptr_result_nullonfailure_ IDxcSourceLocation** location) override;
-    HRESULT STDMETHODCALLTYPE GetNullRange(_Outptr_result_nullonfailure_ IDxcSourceRange** location) override;
-    HRESULT STDMETHODCALLTYPE GetRange(
-      _In_ IDxcSourceLocation* start,
-      _In_ IDxcSourceLocation* end,
-      _Outptr_result_nullonfailure_ IDxcSourceRange** location) override;
-    HRESULT STDMETHODCALLTYPE GetDefaultDiagnosticDisplayOptions(
-      _Out_ DxcDiagnosticDisplayOptions* pValue) override;
-    HRESULT STDMETHODCALLTYPE GetDefaultEditingTUOptions(_Out_ DxcTranslationUnitFlags* pValue) override;
-    HRESULT STDMETHODCALLTYPE CreateUnsavedFile(
-      _In_ LPCSTR fileName, _In_ LPCSTR contents, unsigned contentLength,
-      _Outptr_result_nullonfailure_ IDxcUnsavedFile** pResult) override;
+  HRESULT STDMETHODCALLTYPE CreateIndex(_Outptr_result_nullonfailure_ IDxcIndex** index) override;
+  HRESULT STDMETHODCALLTYPE GetNullLocation(_Outptr_result_nullonfailure_ IDxcSourceLocation** location) override;
+  HRESULT STDMETHODCALLTYPE GetNullRange(_Outptr_result_nullonfailure_ IDxcSourceRange** location) override;
+  HRESULT STDMETHODCALLTYPE GetRange(
+    _In_ IDxcSourceLocation* start,
+    _In_ IDxcSourceLocation* end,
+    _Outptr_result_nullonfailure_ IDxcSourceRange** location) override;
+  HRESULT STDMETHODCALLTYPE GetDefaultDiagnosticDisplayOptions(
+    _Out_ DxcDiagnosticDisplayOptions* pValue) override;
+  HRESULT STDMETHODCALLTYPE GetDefaultEditingTUOptions(_Out_ DxcTranslationUnitFlags* pValue) override;
+  HRESULT STDMETHODCALLTYPE CreateUnsavedFile(
+    _In_ LPCSTR fileName, _In_ LPCSTR contents, unsigned contentLength,
+    _Outptr_result_nullonfailure_ IDxcUnsavedFile** pResult) override;
 };
 };
 
 
 class DxcSourceLocation : public IDxcSourceLocation
 class DxcSourceLocation : public IDxcSourceLocation
@@ -227,13 +227,12 @@ private:
   CXSourceLocation m_location;
   CXSourceLocation m_location;
 public:
 public:
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
+  DXC_MICROCOM_TM_CTOR(DxcSourceLocation)
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   {
   {
     return DoBasicQueryInterface<IDxcSourceLocation>(this, iid, ppvObject);
     return DoBasicQueryInterface<IDxcSourceLocation>(this, iid, ppvObject);
   }
   }
 
 
-  DxcSourceLocation();
-  ~DxcSourceLocation();
   void Initialize(const CXSourceLocation& location);
   void Initialize(const CXSourceLocation& location);
   static HRESULT Create(const CXSourceLocation& location, _Outptr_result_nullonfailure_ IDxcSourceLocation** pObject);
   static HRESULT Create(const CXSourceLocation& location, _Outptr_result_nullonfailure_ IDxcSourceLocation** pObject);
 
 
@@ -258,13 +257,12 @@ private:
   CXSourceRange m_range;
   CXSourceRange m_range;
 public:
 public:
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
+  DXC_MICROCOM_TM_CTOR(DxcSourceRange)
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   {
   {
     return DoBasicQueryInterface<IDxcSourceRange>(this, iid, ppvObject);
     return DoBasicQueryInterface<IDxcSourceRange>(this, iid, ppvObject);
   }
   }
 
 
-  DxcSourceRange();
-  ~DxcSourceRange();
   void Initialize(const CXSourceRange& range);
   void Initialize(const CXSourceRange& range);
   static HRESULT Create(const CXSourceRange& range, _Outptr_result_nullonfailure_ IDxcSourceRange** pObject);
   static HRESULT Create(const CXSourceRange& range, _Outptr_result_nullonfailure_ IDxcSourceRange** pObject);
 
 
@@ -283,13 +281,12 @@ private:
   CXTranslationUnit m_tu;
   CXTranslationUnit m_tu;
 public:
 public:
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
+  DXC_MICROCOM_TM_CTOR(DxcToken)
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   {
   {
     return DoBasicQueryInterface<IDxcToken>(this, iid, ppvObject);
     return DoBasicQueryInterface<IDxcToken>(this, iid, ppvObject);
   }
   }
 
 
-  DxcToken();
-  ~DxcToken();
   void Initialize(const CXTranslationUnit& tu, const CXToken& token);
   void Initialize(const CXTranslationUnit& tu, const CXToken& token);
   static HRESULT Create(const CXTranslationUnit& tu, const CXToken& token, _Outptr_result_nullonfailure_ IDxcToken** pObject);
   static HRESULT Create(const CXTranslationUnit& tu, const CXToken& token, _Outptr_result_nullonfailure_ IDxcToken** pObject);
 
 
@@ -306,12 +303,13 @@ private:
     CXTranslationUnit m_tu;
     CXTranslationUnit m_tu;
 public:
 public:
     DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
     DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
+    DXC_MICROCOM_TM_ALLOC(DxcTranslationUnit)
     HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
     HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
     {
     {
       return DoBasicQueryInterface<IDxcTranslationUnit>(this, iid, ppvObject);
       return DoBasicQueryInterface<IDxcTranslationUnit>(this, iid, ppvObject);
     }
     }
 
 
-    DxcTranslationUnit();
+    DxcTranslationUnit(IMalloc *pMalloc);
     ~DxcTranslationUnit();
     ~DxcTranslationUnit();
     void Initialize(CXTranslationUnit tu);
     void Initialize(CXTranslationUnit tu);
 
 
@@ -358,13 +356,12 @@ private:
   CXType m_type;
   CXType m_type;
 public:
 public:
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
+  DXC_MICROCOM_TM_CTOR(DxcType)
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override
   {
   {
     return DoBasicQueryInterface<IDxcType>(this, iid, ppvObject);
     return DoBasicQueryInterface<IDxcType>(this, iid, ppvObject);
   }
   }
 
 
-  DxcType();
-  ~DxcType();
   void Initialize(const CXType& type);
   void Initialize(const CXType& type);
   static HRESULT Create(const CXType& type, _Outptr_result_nullonfailure_ IDxcType** pObject);
   static HRESULT Create(const CXType& type, _Outptr_result_nullonfailure_ IDxcType** pObject);
 
 
@@ -380,12 +377,12 @@ private:
   CXCodeCompleteResults* m_ccr;
   CXCodeCompleteResults* m_ccr;
 public:
 public:
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
+  DXC_MICROCOM_TM_CTOR(DxcCodeCompleteResults)
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid,
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid,
                                            void **ppvObject) override {
                                            void **ppvObject) override {
     return DoBasicQueryInterface<IDxcCodeCompleteResults>(this, iid, ppvObject);
     return DoBasicQueryInterface<IDxcCodeCompleteResults>(this, iid, ppvObject);
   }
   }
 
 
-  DxcCodeCompleteResults();
   ~DxcCodeCompleteResults();
   ~DxcCodeCompleteResults();
   void Initialize(CXCodeCompleteResults* ccr);
   void Initialize(CXCodeCompleteResults* ccr);
 
 
@@ -400,13 +397,12 @@ private:
   CXCompletionResult m_cr;
   CXCompletionResult m_cr;
 public:
 public:
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
+  DXC_MICROCOM_TM_CTOR(DxcCompletionResult)
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid,
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid,
                                            void **ppvObject) override {
                                            void **ppvObject) override {
     return DoBasicQueryInterface<IDxcCompletionResult>(this, iid, ppvObject);
     return DoBasicQueryInterface<IDxcCompletionResult>(this, iid, ppvObject);
   }
   }
 
 
-  DxcCompletionResult();
-  ~DxcCompletionResult();
   void Initialize(const CXCompletionResult& cr);
   void Initialize(const CXCompletionResult& cr);
 
 
   HRESULT STDMETHODCALLTYPE GetCursorKind(_Out_ DxcCursorKind *pResult) override;
   HRESULT STDMETHODCALLTYPE GetCursorKind(_Out_ DxcCursorKind *pResult) override;
@@ -420,13 +416,12 @@ private:
   CXCompletionString m_cs;
   CXCompletionString m_cs;
 public:
 public:
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
   DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
+  DXC_MICROCOM_TM_CTOR(DxcCompletionString)
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid,
   HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid,
                                            void **ppvObject) override {
                                            void **ppvObject) override {
     return DoBasicQueryInterface<IDxcCompletionString>(this, iid, ppvObject);
     return DoBasicQueryInterface<IDxcCompletionString>(this, iid, ppvObject);
   }
   }
 
 
-  DxcCompletionString();
-  ~DxcCompletionString();
   void Initialize(const CXCompletionString& cs);
   void Initialize(const CXCompletionString& cs);
 
 
   HRESULT STDMETHODCALLTYPE GetNumCompletionChunks(_Out_ unsigned *pResult) override;
   HRESULT STDMETHODCALLTYPE GetNumCompletionChunks(_Out_ unsigned *pResult) override;