Use LLVM/Clang to find fclose() calls in an Xcode project - xcode

I would like to learn how I might programmatically integrate with LLVM/Clang to find all of the fclose() calls in my Xcode project. I realize I can accomplish this via normal text searching but this is just the first step in a more detailed problem.

You can write function pass and find the name of the function as below:
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
namespace {
struct Hello : public FunctionPass {
static char ID;
Hello() : FunctionPass(ID) {}
virtual bool runOnFunction(Function &F) {
errs() << "Hello: ";
errs().write_escaped(F.getName()) << '\n';
return false;
}
};
}
char Hello::ID = 0;
static RegisterPass<Hello> X("hello", "Hello World Pass", false, false);
Call this pass from opt using opt -hello input.ll and you will get the names of all functions printed. Change the logic in the above code to find your required function. See the following link for more details on writing passes:
http://llvm.org/docs/WritingAnLLVMPass.html

Related

Is it possible to include code only inside one class?

I hope I can explain myself.
Supose I have next:
File "A.h":
#include "C.h"
public class A{
// Some code...
}
File "B.h":
#include "A.h"
public class B{
A a = new A(); //With this line I mean I'm using one instance of "A" inside "B.h"
//Some code...
}
Is it possible to include "C.h" ONLY inside "A.h"?
My problem is that the code I've included is giving me a lot of conflicts with usual functions. It's not an option to correct conflicts one by one, because there is a huge set of them. Also, my "C.h" code included is only a test code: after some tests, I will delete the include line.
Is there any way of 'bubbling' my include?
Thank you in advance.
EDIT: A.h and B.h are on the same namespace.
Is it possible to include "C.h" ONLY inside "A.h"?
No. Not to my knowledge.
If you have name conflicts, just include C.h within an other namespace, as #user202729 proposed. This can help.
But I guess you use C in A for tests and you cannot use it in C in A without the implementation which is not compatible to C++Cli or content from B.h.
We used the pimpl ideom (pointer to implementation).
Example:
c++/clr currently does not allow do be included directly and that's why sometimes you cannot use libraries you want to use (like C.h), because they do rely on the support of .
This is my C.h ( used by all the other headers)
struct LockImpl; // forward declaration of C.
class C
{
public:
C();
virtual ~C();
public:
void Lock() const;
void Unlock() const;
LockImpl* _Lock;
};
This is my C.cpp (compiled without /clr )
#include <mutex>
struct LockImpl
{
std::mutex mutex;
};
C::C() : _Lock(new LockImpl()) {}
C::~C() { delete _Lock; }
void C::Lock() const
{
_Lock->mutex.lock();
}
void C::Unlock() const
{
_Lock->mutex.unlock();
}
A.h
#include "C.h"
public class A{
C c;
void someMethod()
{
c.Lock() // I used another template for a RAII pattern class.
c.Unlock()
}
}

How to call a method added to the windows native module of Appelerator Platform

We write to ask how to correctly call a method of Titanium Appelerator native module.
Our development environment is as the following:
Titanium SDK :5.2.0
Appcelerator CLI: 5.2.0
TabletPC:Windows10pro
Our module has been developed with the following procedures;
(1) we created a module referring to the document of https://github.com/appcelerator/titanium_mobile_windows#module-development
An additional method aaa() was added to the module.
cd MY_WORKSPACE
ti create -p windows -t module //create a module project
cd MY_MODULE_NAME/windows
ti build -p windows -T ws-local //build my module project
(2) A windows’ application was created to call the above module, and there was no error while we call the module from the application. However, while we call the method aaa(), Titanium error message as the following occurred;
“aaa() method does not exist”.
In the Titanium windows platform for windows10, the dll for module was created correctly. Then, we are just wondering which parts of our source are not correct;
(1) The definition of a method aaa()is not correct.
(2) The source code to call the native module is not correct.
(3) The source code to call the aaa() method is not correct.
We attached our source code as the following and thank you in advance for your comment and advice.
The code is as below.
windows/src/JpNativeModuleExample.cpp
#include "JpNativeModuleExample.hpp"
#include "Titanium/detail/TiImpl.hpp"
namespace Jp
{
NativeModuleExample::NativeModuleExample(const JSContext& js_context) TITANIUM_NOEXCEPT
: JSExportObject(js_context)
{
TITANIUM_LOG_DEBUG("NativeModuleExample::ctor Initialize");
}
void NativeModuleExample::postInitialize(JSObject& js_object)
{
}
void NativeModuleExample::postCallAsConstructor(const JSContext& js_context, const std::vector<JSValue>& arguments)
{
}
//add new method
void NativeModuleExample::aaa() TITANIUM_NOEXCEPT
{
}
void NativeModuleExample::JSExportInitialize()
{
JSExport<NativeModuleExample>::SetClassVersion(1);
JSExport<NativeModuleExample>::SetParent(JSExport<JSExportObject>::Class());
}
}
windows/include/JpNativeModuleExample.hpp
#ifndef _JPNATIVEMODULEEXAMPLE_HPP_
#define _JPNATIVEMODULEEXAMPLE_HPP_
#include "JpNativeModuleExample_EXPORT.h"
#include "Titanium/detail/TiBase.hpp"
#include "Titanium/Module.hpp"
namespace Jp
{
using namespace HAL;
class JPNATIVEMODULEEXAMPLE_EXPORT NativeModuleExample : public JSExportObject, public JSExport<NativeModuleExample>
{
public:
NativeModuleExample(const JSContext&) TITANIUM_NOEXCEPT;
void aaa() TITANIUM_NOEXCEPT;
virtual void postInitialize(JSObject& js_object) override;
virtual void postCallAsConstructor(const JSContext& js_context, const std::vector<JSValue>& arguments) override;
virtual void aaa(); //add new method
virtual ~NativeModuleExample() = default;
NativeModuleExample(const NativeModuleExample&) = default;
NativeModuleExample& operator=(const NativeModuleExample&) = default;
#ifdef TITANIUM_MOVE_CTOR_AND_ASSIGN_DEFAULT_ENABLE
NativeModuleExample(NativeModuleExample&&) = default;
NativeModuleExample& operator=(NativeModuleExample&&) = default;
#endif
static void JSExportInitialize();
};
}
#endif // _JPNATIVEMODULEEXAMPLE_HPP_
sample code
var nativemoduleexample = require('jp.NativeModuleExample');
Ti.API.info("module is => " + nativemoduleexample); //no problem
var aaa = nativemoduleexample.aaa(); //titanium error is displayed
$.index.open();
In order to enable your function, you need to register it using TITANIUM_ADD_FUNCTION at JSExportInitialize(). You might want to check out working examples under TitaniumKit such as Ti.UI.Button and Ti.UI.Window.
void NativeModuleExample::JSExportInitialize()
{
JSExport<NativeModuleExample>::SetClassVersion(1);
JSExport<NativeModuleExample>::SetParent(JSExport<JSExportObject>::Class());
TITANIUM_ADD_FUNCTION(NativeModuleExample, aaa);
}
And then use TITANIUM_FUNCION to define function like this...
TITANIUM_FUNCTION(NativeModuleExample, aaa)
{
aaa();
return get_context().CreateUndefined()
}

Map File contains obsalutely identical addresses for different functions

I am trying to get stack trace in release (optimized) build without pdb-s.
Currently I am trying to retrieve function addresses during my sample program execution using StackWalk64 function and then map generated addresses to the actual function names using map file generated during linking stage. Please note that optimization is turned on.
I see absolutely identical addresses for two different functions in the generated map file.
0001:00000000 ?static_function_call#MyTest##SAXXZ 00401000 f i main.obj
0001:00000000 ?call_1#MyTest##QAEXXZ 00401000 f i main.obj
What could be the reason of such a thing, can it be due to optimization ? Then how can this functions be distinguished ?
EDIT:
Here is the function bodies
#include <iostream>
#include <windows.h>
#include <dbghelp.h>
class __declspec(dllexport) MyTest
{
public:
static void static_function_call()
{
}
void call_1()
{
static_function_call();
};
};
int main( void )
{
try
{
MyTest obj;
obj.call_1();
}
catch( ... )
{
}
return ( 0 );
}
Thank you,
-Grigor

Initializing class with a lambda passed into constructor, C++11

Lets consider following example:
#include <functional>
#include <iostream>
using namespace std;
class Caller {
public:
Caller(function<void()> callback) {
callback();
}
};
main() {
#if defined(ONELINER)
Caller caller = [] { cout << "test"; };
#else
function<void()> fun = [] { cout << "test"; };
Caller caller(fun);
#endif // defined(ONELINER)
}
If we simply try to compile it (with -std=c++11 flag) it will happily finish, and display test when run. However if we define ONELINER macro compilation will fail with:
prog.cpp: In function 'int main()':
prog.cpp:17:40: error: conversion from 'main()::<lambda()>' to non-scalar type 'Caller' requested
Caller caller = [] { cout << "test"; };
I understand that this is caused by the fact that there is implicit conversion from lambda to std::function and then implicit conversion from std::function to Caller, and we cannot perform 2 conversions at the same time.
Is it somehow possible to make syntax Class object = lambda; work? I'm asking because I played recently with writing my own small testing framework for educational reasons and I thought that this:
UNIT_TEST(test_name) {
// test content
};
is much more elegant than
UNIT_TEST_BEGIN(test_name)
// unit test
UNIT_TEST_END()
The former can be achieved with lambdas passed into the UnitTest constructor. But with problem that I described I had to use dirty workaround like:
#define UNIT_TEST(test_name) \
::std::function<void(::Helper*)> test_name_helper; \
::UnitTest test_name ## _test = \
test_name_helper = \
[&] (::Helper* helper)
and it doesn't look elegant at all. But even if this can be done without lambdas I'm still intrigued whether Class object = lamda; syntax can be achieved.
Modify the constructor as such:
template<typename CB>
Caller(CB callback) {
callback();
}
That will allow it to accept any callable argument, be it a lambda, a std::function, function pointer or functor.
Unfortunately the constructor will also accept any other type as well, but give a compiler error when callback can't be "called" like a function.

How to call c++ program in Python

I have written simple HelloWorld class. and write a boost python wrapper.and debug the code as DLL.My question is how can i expose this code in python and use greet function.I tried by giving the path in sys.path.insert. but not able to get greet function. The code i treid is below.
Thanks for help.
#include<boost/python.hpp>
using namespace std;
using namespace boost::python;
class World
{
public:
string msg;
void set(string msg)
{
this->msg=msg;
}
string greet()
{
return msg;
}
};
BOOST_PYTHON_MODULE(ExpsoingClasses)
{
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}
At least on my system, I had to rename the library file from ExpsoingClasses.dll to ExpsoingClasses.pyd before I could import it in Python. Once you do that, this should work:
import ExpsoingClasses
retVal = ExpsoingClasses.World()
retVal.set('hello world')
print retVal.greet()

Resources