TEST_F(UnifiedVarRenamerTests,
WhenUseDebugNamesIsFalseDoNotUseDebugNames) {
	// Set-up the module.
	//
	// int g; // from debug info
	// int h;
	//
	// void test(int p, int m) { // p has name from debug info
	//     int a;
	//     int b; // from debug info
	// }
	//
	ShPtr<Variable> varG(Variable::create("g", IntType::create(32)));
	module->addGlobalVar(varG);
	module->addDebugNameForVar(varG, varG->getName());
	ShPtr<Variable> varH(Variable::create("h", IntType::create(32)));
	module->addGlobalVar(varH);
	ShPtr<Variable> varP(Variable::create("p", IntType::create(32)));
	testFunc->addParam(varP);
	module->addDebugNameForVar(varP, varP->getName());
	ShPtr<Variable> varM(Variable::create("m", IntType::create(32)));
	testFunc->addParam(varM);
	ShPtr<Variable> varA(Variable::create("a", IntType::create(32)));
	testFunc->addLocalVar(varA);
	ShPtr<Variable> varB(Variable::create("b", IntType::create(32)));
	testFunc->addLocalVar(varB);
	module->addDebugNameForVar(varB, varB->getName());
	ShPtr<VarDefStmt> varDefB(VarDefStmt::create(varB));
	ShPtr<VarDefStmt> varDefA(VarDefStmt::create(varA, ShPtr<Expression>(), varDefB));
	testFunc->setBody(varDefA);

	// Setup the renamer (do not use debug names).
	INSTANTIATE_VAR_NAME_GEN_AND_VAR_RENAMER(UnifiedVarRenamer, false);

	// Do the renaming.
	varRenamer->renameVars(module);

	// We expect the following output:
	//
	// int g1;
	// int g2;
	//
	// void test(int a1, int a2) {
	//     int v1;
	//     int v2;
	// }
	//
	// Globals:
	VarSet globalVarsSet(module->getGlobalVars());
	ASSERT_EQ(2, globalVarsSet.size());
	// We have to sort the variables to ease the checking.
	VarVector globalVarsVector(globalVarsSet.begin(), globalVarsSet.end());
	sortByName(globalVarsVector);
	ShPtr<Variable> var1(globalVarsVector[0]);
	EXPECT_EQ("g1", var1->getName());
	ShPtr<Variable> var2(globalVarsVector[1]);
	EXPECT_EQ("g2", var2->getName());
	// Parameters:
	VarVector params(testFunc->getParams());
	ASSERT_EQ(2, params.size());
	ShPtr<Variable> par1(params.front());
	EXPECT_EQ("a1", par1->getName());
	ShPtr<Variable> par2(params.back());
	EXPECT_EQ("a2", par2->getName());
	// Locals:
	EXPECT_EQ("v1", varDefA->getVar()->getName());
	EXPECT_EQ("v2", varDefB->getVar()->getName());
}
示例#2
0
int _tmain(int argc, _TCHAR* argv[])
{
	//BSTR的使用
	{
		BSTR bstrA = SysAllocString(L"Hello BSTR");
		BSTR bstrB = SysAllocStringLen(bstrA, SysStringLen(bstrA));
		SysFreeString(bstrA);
		SysFreeString(bstrB);
	}

	//用VARIANT保存LONG
	{
		VARIANT var;
		VariantInit(&var);

		var.vt = VT_I4;
		var.lVal = 100;

		VariantClear(&var);

		int i = 0;
		i++;
	}

	//用VARIANT保存FLOAT
	{
		VARIANT var;
		VariantInit(&var);
		
		var.vt = VT_R4;
		var.fltVal = 1.23f;

		VariantClear(&var);
	}

	//用VARIANT保存BSTR
	{
		VARIANT var;
		VariantInit(&var);
		
		var.vt = VT_BSTR;
		var.bstrVal = SysAllocString(L"Hello World");

		VariantClear(&var);
	}

	//用VARIANT保存布尔类型
	{
		VARIANT var;
		VariantInit(&var);

		var.vt = VT_BOOL;
		var.boolVal = VARIANT_FALSE;

		VariantClear(&var);
	}

	//从VARIANT读取相应类型的值
	{
		VARIANT var;
		VariantInit(&var);

		var.vt = VT_I4;
		var.lVal = 100;

		if (var.vt == VT_I4)
		{ 
			LONG lValue = var.lVal;
		}
		else if (var.vt == VT_R4)
		{
			FLOAT fValue = var.fltVal;
		}
		else if (var.vt == VT_BSTR)
		{
			BSTR bstrValue = var.bstrVal;
		}
		else if (var.vt == VT_BOOL)
		{
			VARIANT_BOOL varbValue = var.boolVal;
		}

		VariantClear(&var);
	}

	//COM数据类型的转换,LONG转成FLOAT
	{
		VARIANT var;
		VariantInit(&var);

		var.vt = VT_I4;
		var.lVal = 100;

		VariantChangeType(&var,&var,0, VT_R4);

		if (var.vt == VT_R4)
		{
			FLOAT fValue = var.fltVal;
		}

		VariantClear(&var);
	}

	//COM数据类型的转换,LONG转成BSTR
	{
		VARIANT var;
		VariantInit(&var);

		var.vt = VT_I4;
		var.lVal = 100;

		VariantChangeType(&var,&var,0, VT_BSTR);

		if (var.vt == VT_BSTR)
		{
			BSTR fValue = var.bstrVal;
		}

		VariantClear(&var);
	}

	//CComVariant的构造方法
	{
		VARIANT varA;
		CComVariant varB;
		CComVariant varC(varA);
		CComVariant varD(varB);
		CComVariant varE(L"Hello CComVariant");
		CComVariant varF("CComVariant");
		CComVariant varG(true);
		CComVariant varH(100L);
		CComVariant varI(1.23f);
		//....

	}

	//CComVariant的赋值方法
	{
		VARIANT varA;
		CComVariant varB;
		CComVariant varC;
		varC = varA;
		varC = varB;
		varC = true;
		varC = 100L;
		varC = 1.23f;
		varC = L"Hello CComVariant";
		varC = "Hello CComVariant";
		//....
	}

	//CComVariant与VARIANT的关联
	{
		VARIANT varA;
		VariantInit(&varA);
		varA.vt = VT_I4;
		varA.lVal = 100;
		
		CComVariant varB;

		CComVariant varC(100L);
		
		VARIANT varD;
		VariantInit(&varD);

		//Attach
		varB.Attach(&varA);		//之后无需调用VariantClear(&varA);

		//Detach
		varC.Detach(&varD);
		VariantClear(&varD);	//需要调用VariantClear(&varD);
	}

	return 0;
}