Clang code complete for not well-formed code? - c++

I'm working on adding code complete via Clang to text editor to make it IDE.
The source code:
struct s {
int a;
float b;
};
void main() {
s var;
var.
The problem is that code complete for the position after dot returns nothing and if i add } at the end and retry code complete for the position after dot it shows the correct list.
I understand that the main function definition should be closed, but users frequently type chars one-by-one and don't want to close function first and then return back to variable and then get code complete. How can that be walked-around to avoid go back/return?
My idea was to get diagnostics and add } if i get according diagnostics, but it's unwished walk-around. Can Clang be smart enough to make it itself?

Related

'identifier undefined' in C++11 for-loop with USTRUCT

I am implementing logging functionality in Unreal Engine 4.27 (in C++). A key part of my code is a function that is called once per game-tick. This function is responsible for iterating over an array of actors that I would like to log data for, checking whether a new log entry should be written at this point in time and calling the necessary functions to do that.
I am iterating over elements of a TArray of UStructs: LogObject->LoggingInfo = TArray<FActorLoggingInformation>. This array is defined as a UProperty of LogObject. In the loop I have to change the values of the elements so I want to work with the original items and "label" the current item as "ActorLoggingInfo". I have seen this done generally in cpp and also with TArrays. And yet my code does not work, there is no error message, but ActorLoggingInfo is undefined, thus the if-condition is never met.
This is the for-loop:
for (FActorLoggingInformation& ActorLoggingInfo : LogObject->LoggingInfo) {
if (ActorLoggingInfo.LogNextTick == true) {
ActorLoggingInfo.LogNextTick = false;
...
}
...
}
This is the definition of FActorLoggingInformation:
USTRUCT(BlueprintType)
struct FActorLoggingInformation
{
GENERATED_BODY()
public:
FActorLoggingInformation()
{
}
FActorLoggingInformation(int32 LogTimer, AActor* Actor, FString LogName)
{
this->LogTimer = LogTimer;
this->LogNextTick = false;
...
}
// Specifies Logging Frequency in ms
UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
int32 LogTimer;
bool LogNextTick;
...
};
This is the debugger at run-time:
Additional Notes:
1. Something that consistently works for me is omitting the &, using:
for (FActorLoggingInformation ActorLoggingInfo : LogObject->LoggingInfo)
However, this is creating useless duplicates on a per-tick basis and complicates applying changes to the original objects from within in the for-loop, so it is not a viable option.
2. I have also tried auto& instead of FActorLoggingInformation& as used in the examples above, but I encountered the same issue, so I thought it would be best to be as explicit as possible.
I would be very thankful if you had any ideas how I can fix this :)
Thanks in advance!
Thanks to Avi Berger for helping me find my problem!
In fact, ActorLoggingInfo was actually never undefined and the code within the body of the if-clause was also executed (it just didn't do what it was intended to do).
When stepping through the code in the debugger it never showed the steps within the if-body and ActorLoggingInfo was shown as undefined so when no logs were written, I assumed it was something to do with that instead of my output function not working properly. So lesson learnt, do not blindly trust the debugger :)

Why storage fault in regex destructor?

I am getting a storage fault when my code destructs a regex and I am mystified as to the reason. I suspect I am missing something stupid about regex.
A little background: I am a reasonably experienced C++ developer but this is my first adventure with the regex class. My environment is a little unusual: I edit and alpha test in MS Visual C++ and then take the code to another environment. The other environment is fully Posix-compliant and just happens to be an IBM mainframe. The code works fine on Windows but fails every time on the mainframe. The problem is not something fundamental to my mixed environment: I have been working in this pair of environments in this way for years with complete C++ success.
I define the regex in the class declaration:
#include <regex>
...
class FilterEvalEGNX : public FilterEval
{
...
std::tr1::basic_regex<char> regexObject;
// I also tried plain regex with no difference
Subsequently in the class implementation I assign a pattern to the regex. The code should be more complex than this but I simplified it down to assigning a static string to eliminate any possible side effects from the way the string would be handled in real life.
std::tr1::regex::flag_type flags = std::tr1::regex::extended;
// I have also tried ECMA and it made no difference
try
{
static const char pat[] = "(ISPPROF|SPFTEMP)";
regexObject.assign(pat, flags);
}
catch (std::tr1::regex_error &e)
{
// handle regex error
}
That works without error. Of course, there is subsequent pattern matching code but it is not part of the problem: if I destruct the class immediately after the above code I get the storage fault.
I don't do anything to the regex in my class destructor. The rest of the class has been working for years; I am adding the regex now. I think some "external" overlay of the regex is unlikely.
Here is the traceback of the calls leading up to the fault:
std::tr1::_EBCDIC::_Destroy(std::tr1::_EBCDIC::_Node_base*)
+00000066 40 CRTE128N Exception
std::tr1::_EBCDIC::basic_regex<char,std::tr1::_EBCDIC::regex
+000000C8 2022 FilterEvalEGNX.C Call
std::tr1::_EBCDIC::basic_regex<char,std::tr1::_EBCDIC::regex
+0000007C 1913 FilterEvalEGNX.C Call
FilterEvalEGNX::~FilterEvalEGNX()
The code in the vicinity of line 1913 of regex is
~basic_regex()
{ // destroy the object
_Tidy();
}
The code in the vicinity of line 2022 of regex is
void _Tidy()
{ // free all storage
if (_Rep && --_Rep->_Refs == 0)
_Destroy(_Rep);
_Rep = 0;
}
_Destroy() appears to be implemented in the run-time and I do not think I have the source.
Any ideas? Thanks,
Believe it or not, it appears to be a bug in the C++ runtime. I tweaked my simple example and now I can duplicate the problem in a 15-line main(). I am going to run this by some peers and then report it to IBM. They actually fix this stuff! They don't just respond with "yes, you have found an issue."

[Unreal Engine 4]Making Node with 2 exec working together

So here is my issue. I'm trying to create function, which goes on straight away, but also have second exec output, which goes after let's say completing loop.
I tried to make this work with this: thread i googled.
However my issue is when i tried doing it with accepted answer provided i got this error:
E0434 a reference of type "TEnumAsByte<EMyEnum> &" (not const-qualified) cannot be initialized with a value of type "EMyEnum"
Going furthere below there is second answer, which work but it always goes off form last possible pin.In case I show below it always fire "FinishOutput". Is there any way i can force code to output from both pins i provide? Here is how it looks in my code:
.h file
UENUM(BlueprintType)
enum class EMyEnum : uint8
{
Output,
FinishOutput
};
UFUNCTION(BLueprintCallable, Category = "Test", Meta = (ExpandEnumAsExecs = "Branches"))
static void OutputTest(TEnumAsByte<EMyEnum>& Branches);
.cpp file
void UAudioController::OutputTest(TEnumAsByte<EMyEnum>& Branches)
{
Branches = EMyEnum::Output;
//some code to execute before second output
Branches = EMyEnum::FinishOutput;
}
I would make a Macro since it can have multiple Exec outputs. This is in blueprint, not code.

Cursor placement when calling a method with void parameters

This is a fairly straight forward question but I am unable to find an answer about this specific type of formatting. I'm looking for a way to modify where eclipse places the cursor after using its content assist to complete a method call based on whether or not the method has any parameters.
To illustrate what exactly I'm talking about lets consider a simple c++ class like so:
class Example
{
public:
int voidParams()
{
//do something
return 42;
};
int nonVoidParams(int a)
{
//do something else
return a*a;
};
};
And at some point I created an instance of the class Example ex;
Now within eclipse if I started typing ex.nonV and I told eclipse to auto complete it would enter in ex.nonVoidParams() and after doing this my cursor would be inside the parenthesis like so ex.nonVoidParams(|) where | is my cursor. This makes sense and is useful since I need to give this particular method an argument.
Hopefully none of what I just said is new to anyone and is all pretty straight forward. This is where my question comes in. Having my cursor be placed within the parenthesis of a method call is only useful if that method takes parameters. If I were to type ex.voi and let eclipse auto complete to ex.voidParams() my cursor would be inside the parenthesis like so ex.voidParams(|) where | is my cursor. This isn't very useful since there is nothing for me to enter there.
I would like to know if there is a way to setup eclipse so, given the above examples, if it auto completes a method with void parameters such as ex.voidParams() it places the cursor after the method call like so ex.voidParams()| again where | is my cursor.
I'm not super familiar with customizing eclipse but I feel like there should be a way to do this since if eclipse is auto completing the method call it should know what its parameters are and be able to adjust its formatting from there.
Oh and this will probably be asked at some point, I'm currently using Eclipse CDT version 4.2.0 (Juno service Release 2).
Go to Window->Preferences->C++->Editor->Content Assist->Advanced, pick the "Parsing-based Proposals" instead of "Parsing-based Proposals (Task-Focused)".

Visual Studio Breakpoint Macro to modify a value?

I'm debugging an application (C++), and I've found a point in the code where I want to change a value (via the debugger). So right now, I've got a breakpoint set, whereupon I do:
Debugger reaches breakpoint
I modify the variable I want to change
I hit F5 to continue running
lather, rinse, repeat
It's hitting this breakpoint a lot, so I would like to automate this. I would like to set the Breakpoint to run a macro, and continue execution.
However, I have no experience writing VisualStudio macros, so I don't know the commands for modifying a variable of the executing program. I've looked around, but haven't found anything helpful online so far.
I found how to do this with a macro. Initially, I tried using Ctrl-Shift-R to record a macro of keystrokes, but it stopped recording when I did Ctrl-Alt-Q. But I was able to edit the macro to get it to work. So here's what I did, in case anyone else wants to do something similar.
Tools -> Macros -> Macro Explorer
Right Click -> New macro
Public Module RecordingModule
Sub setvalue()
DTE.Debugger.ExecuteStatement("variable_name=0")
End Sub
End Module
This macro will execute the assignment statement, setting my variable (in this case, making it a NULL pointer).
Right Click on a BreakPoint -> When Hit...
Check "Run a macro"
Select Macros.MyMacros.RecordingModule.setvalue
Check "Continue execution"
Click OK
Then, I was able to run my program, automatically adjusting a pointer to NULL as it went. This was very useful for testing, and did not require recompiling.
Looking for similar today and found that you can also use the 'Print a message:' option instead of a macro. Values from code can be printed by placing them inside {}. The key is that VS will also evaluate the content as an expression - so {variable_name=0} should achieve the same as the macro example.
If you are think of a macro in the same way as Microsoft excel, then you're out of luck. It doesn't quite work that way.
In C++, a macro refers to a small inline function created with #define. It is a preprocessor, so a macro is like using a replace on all its references with its body.
For example:
#define add(a,b) ((a)+(b))
int main() {
int a=3, b=4, c=5, d=6, e, f;
d = add(a,b);
e = add(c,d);
}
Would like to the c++ compiler as:
int main() {
int a=3, b=4, c=5, ...;
d = ((a)+(b));
e = ((c)+(d));
}
Now, back to your question. If the variable is within scope at this breakpoint, just set it from within your code:
myVar = myValue;
If it is not, but it is guaranteed to exist, you may need a little hack. Say that this variable is an int, make a global int pointer. If this variable is static, make sure to set it to its address, and back to NULL inside it's scope. If it is dynamic, you may need some extra work. Here is an example:
int* globalIntPointer;
void func() {
*globalIntPointer = 3;
//...
}
int main() {
int a = 5;
globalIntPointer = &a;
func();
//...
globalIntPointer = NULL; // for safety sake
return 0;
}
You can execute a VS macro when a breakpoint is hit (open the breakpoints window, right click on the breakpoint in question, and select "When Hit..." off the popup menu). I'm less certain about writing a macro that modifies a variable of the program under debug though -- I've never done that, and a quick try with attempting to record a macro to do it doesn't seem to work (all it records is activating the right window, not changing the value).
Select "Condition..." and write an assignment for the variable in question in the "Condition:" textbox. This will naturally resolve to "true" with it not being an actual conditional test. Therefore, the breakpoint is never hit, and your variable has been set accordingly.