Applying to more selective programs after having been diagnosed with a long-term mental condition I have discovered recently that I had ADHD, and possibly some other mental condition, which explain some past academic difficulties. This gave me hope that with treatment (which I will start soon) and newfound motivation, I can improve my academic results dramatically and aim for a PhD.

My opinion for now is that the plan that gives me the best chances would be to get a second master's degree containing more theory compared to my current one (computer engineering). I'm aiming for some universities I have visited, which are quite highly regarded compared to my current one.

My results have had ups and downs over the years, and would be clearly insufficient without a very good justification, which I believe my mental health problems to be. I have documentation of the mental health problems I suffered from during part of my studies. I don't plan to use these as an argument for admission, but providing that I get better results in the next semester after getting medical help, I am hoping that the admission committee won't disqualify me instantly based on my past results, and give more importance to the next semester.

One additional difficulty is that I'm nearing the end of my current degree, so I won't have another occasion to get good results. I worry that getting good grades in the last months could be insufficient to offset my less-than-stellar record.

Is a very late mental disorder diagnosis followed by a significant improvement in results likely to save my applications to selective programs? Should I lower my ambitions for the moment?
It's certainly a huge relief to finally know, after years or decades of struggling, what the underlying problem is. But people with ADHD tend to assume that a diagnosis and treatment is a magic bullet that will suddenly make them like everyone else. I think part of this is ADHD tendencies and part is internalizing the erroneous belief that if you just tried harder you'd stop screwing up. I don't have any first-hand experience with ASD, but the issues seem to be similar. 

So here's my first observation: A diagnosis does not "fix" ADHD. A diagnosis together with medication and therapy or coaching does not "fix" ADHD. Right now you're at the starting point and you've been pointed in the right direction, but it's probably going to take some time and experimentation to find out exactly what you need to successfully manage advanced academic work. Expect that what you need might be really different than what is ordinarily offered in an academic setting. Don't beat yourself up trying to be like the neurotypicals the work is designed for. 

My second observation: As you can see from the answers already given, many professors and programs take the attitude that learning disorders/differences are the student's problem and it's not their job to deal with them (except maybe in a very superficial legally-mandated way). This attitude might vary by country--my own experiences in the US have been pretty dismal. So even once you know what you need, and even though the law is on your side, you might find yourself fighting a losing battle against these attitudes to get it. So it's also important to be sure that you're entering an environment that sets you up for success. 

I honestly don't know the answer to this second problem. I might post a related question here later to see what the ASE community thinks. I don't mean to sound pessimistic, but the fact is that the academic community is on the whole not particularly welcoming to people with learning differences, and ADHD is particularly misunderstood. The trick here is to find the exceptions who will provide the conditions you need to flourish.