Browse Source

Fixed some preprocessor directive tests.

pull/45/merge
Mike Krüger 12 years ago
parent
commit
ae6b9e27cb
  1. 34
      ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/PreProcessorDirective.cs
  2. 34
      ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs
  3. 5
      ICSharpCode.NRefactory.CSharp/Parser/mcs/cs-tokenizer.cs
  4. 2
      ICSharpCode.NRefactory.CSharp/Parser/mcs/driver.cs
  5. 30
      ICSharpCode.NRefactory.CSharp/Parser/mcs/location.cs
  6. 6
      ICSharpCode.NRefactory.CSharp/Parser/mcs/namespace.cs
  7. 22
      ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/PreprocessorDirectiveTests.cs

34
ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/PreProcessorDirective.cs

@ -24,6 +24,7 @@ @@ -24,6 +24,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
namespace ICSharpCode.NRefactory.CSharp
{
@ -45,6 +46,39 @@ namespace ICSharpCode.NRefactory.CSharp @@ -45,6 +46,39 @@ namespace ICSharpCode.NRefactory.CSharp
Pragma = 11,
Line = 12
}
public class PragmaWarningPreprocssorDirective : PreProcessorDirective
{
public bool Disable {
get;
set;
}
List<int> warningList = new List<int> ();
public IList<int> WarningList {
get {
return warningList;
}
}
public PragmaWarningPreprocssorDirective(TextLocation startLocation, TextLocation endLocation) : base (PreProcessorDirectiveType.Pragma, startLocation, endLocation)
{
}
public PragmaWarningPreprocssorDirective(string argument = null) : base (PreProcessorDirectiveType.Pragma, argument)
{
}
public void AddWarnings(IEnumerable<int> warningCodes)
{
warningList.AddRange(warningCodes);
}
public void AddWarnings(params int[] warningCodes)
{
warningList.AddRange(warningCodes);
}
}
public class PreProcessorDirective : AstNode
{

34
ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs

@ -3650,13 +3650,22 @@ namespace ICSharpCode.NRefactory.CSharp @@ -3650,13 +3650,22 @@ namespace ICSharpCode.NRefactory.CSharp
};
role = Roles.Comment;
} else if (!GenerateTypeSystemMode) {
var directive = special as SpecialsBag.PreProcessorDirective;
if (directive != null) {
newLeaf = new PreProcessorDirective ((ICSharpCode.NRefactory.CSharp.PreProcessorDirectiveType)((int)directive.Cmd & 0xF), new TextLocation (directive.Line, directive.Col), new TextLocation (directive.EndLine, directive.EndCol)) {
Argument = directive.Arg,
Take = directive.Take
};
var pragmaDirective = special as SpecialsBag.PragmaPreProcessorDirective;
if (pragmaDirective != null) {
var pragma = new PragmaWarningPreprocssorDirective(new TextLocation(pragmaDirective.Line, pragmaDirective.Col), new TextLocation(pragmaDirective.EndLine, pragmaDirective.EndCol));
pragma.Disable = pragmaDirective.Disalbe;
pragma.AddWarnings(pragmaDirective.Codes);
newLeaf = pragma;
role = Roles.PreProcessorDirective;
} else {
var directive = special as SpecialsBag.PreProcessorDirective;
if (directive != null) {
newLeaf = new PreProcessorDirective ((ICSharpCode.NRefactory.CSharp.PreProcessorDirectiveType)((int)directive.Cmd & 0xF), new TextLocation (directive.Line, directive.Col), new TextLocation (directive.EndLine, directive.EndCol)) {
Argument = directive.Arg,
Take = directive.Take
};
role = Roles.PreProcessorDirective;
}
}
}
if (newLeaf != null) {
@ -3802,7 +3811,18 @@ namespace ICSharpCode.NRefactory.CSharp @@ -3802,7 +3811,18 @@ namespace ICSharpCode.NRefactory.CSharp
}
conversionVisitor.Unit.FileName = fileName;
conversionVisitor.Unit.ConditionalSymbols = top.Conditionals.Concat (compilerSettings.ConditionalSymbols).ToArray ();
List<string> conditionals = new List<string>();
foreach (var settings in compilerSettings.ConditionalSymbols) {
if (top.Conditionals.ContainsKey(settings) && !top.Conditionals [settings])
continue;
conditionals.Add(settings);
}
foreach (var kv in top.Conditionals) {
if (!kv.Value || compilerSettings.ConditionalSymbols.Contains (kv.Key))
continue;
conditionals.Add(kv.Key);
}
conversionVisitor.Unit.ConditionalSymbols = conditionals;
return conversionVisitor.Unit;
}

5
ICSharpCode.NRefactory.CSharp/Parser/mcs/cs-tokenizer.cs

@ -2456,6 +2456,7 @@ namespace Mono.CSharp @@ -2456,6 +2456,7 @@ namespace Mono.CSharp
//
if (length == pragma_warning_disable.Length) {
bool disable = IsTokenIdentifierEqual (pragma_warning_disable);
sbag.SetPragmaDisable (disable);
if (disable || IsTokenIdentifierEqual (pragma_warning_restore)) {
// skip over white space
while (c == ' ' || c == '\t')
@ -2483,6 +2484,7 @@ namespace Mono.CSharp @@ -2483,6 +2484,7 @@ namespace Mono.CSharp
do {
code = TokenizePragmaNumber (ref c);
if (code > 0) {
sbag.AddPragmaCode (code);
if (disable) {
Report.RegisterWarningRegion (loc).WarningDisable (loc, code, context.Report);
} else {
@ -2821,6 +2823,7 @@ namespace Mono.CSharp @@ -2821,6 +2823,7 @@ namespace Mono.CSharp
}
if ((state & TAKING) != 0) {
sbag.SkipIf ();
ifstack.Push (0);
return false;
}
@ -2830,6 +2833,7 @@ namespace Mono.CSharp @@ -2830,6 +2833,7 @@ namespace Mono.CSharp
return true;
}
sbag.SkipIf ();
ifstack.Push (state);
return false;
}
@ -3609,6 +3613,7 @@ namespace Mono.CSharp @@ -3609,6 +3613,7 @@ namespace Mono.CSharp
if (c == '#') {
if (ParsePreprocessingDirective (false))
break;
sbag.StartComment(SpecialsBag.CommentType.InactiveCode, false, line, 1);
}
sbag.PushCommentChar (c);
directive_expected = false;

2
ICSharpCode.NRefactory.CSharp/Parser/mcs/driver.cs

@ -419,7 +419,7 @@ namespace Mono.CSharp @@ -419,7 +419,7 @@ namespace Mono.CSharp
public ModuleContainer ModuleCompiled { get; set; }
public LocationsBag LocationsBag { get; set; }
public SpecialsBag SpecialsBag { get; set; }
public IEnumerable<string> Conditionals { get; set; }
public IDictionary<string, bool> Conditionals { get; set; }
public object LastYYValue { get; set; }
}

30
ICSharpCode.NRefactory.CSharp/Parser/mcs/location.cs

@ -483,6 +483,16 @@ if (checkpoints.Length <= CheckpointIndex) throw new Exception (String.Format (" @@ -483,6 +483,16 @@ if (checkpoints.Length <= CheckpointIndex) throw new Exception (String.Format ("
}
}
public class PragmaPreProcessorDirective : PreProcessorDirective
{
public bool Disalbe { get; set; }
public List<int> Codes = new List<int> ();
public PragmaPreProcessorDirective (int line, int col, int endLine, int endCol, Tokenizer.PreprocessorDirective cmd, string arg) : base (line, col, endLine, endCol, cmd, arg)
{
}
}
public class PreProcessorDirective : SpecialBase
{
public readonly int Line;
@ -562,7 +572,25 @@ if (checkpoints.Length <= CheckpointIndex) throw new Exception (String.Format (" @@ -562,7 +572,25 @@ if (checkpoints.Length <= CheckpointIndex) throw new Exception (String.Format ("
{
if (inComment)
EndComment (startLine, startCol);
Specials.Add (new PreProcessorDirective (startLine, startCol, endLine, endColumn, cmd, arg));
Specials.Add (cmd == Tokenizer.PreprocessorDirective.Pragma ? new PragmaPreProcessorDirective (startLine, startCol, endLine, endColumn, cmd, arg) : new PreProcessorDirective (startLine, startCol, endLine, endColumn, cmd, arg));
}
[Conditional ("FULL_AST")]
public void SetPragmaDisable(bool disable)
{
var pragmaDirective = Specials [Specials.Count - 1] as PragmaPreProcessorDirective;
if (pragmaDirective == null)
return;
pragmaDirective.Disalbe = disable;
}
[Conditional ("FULL_AST")]
public void AddPragmaCode(int code)
{
var pragmaDirective = Specials [Specials.Count - 1] as PragmaPreProcessorDirective;
if (pragmaDirective == null)
return;
pragmaDirective.Codes.Add (code);
}
public enum NewLine { Unix, Windows }

6
ICSharpCode.NRefactory.CSharp/Parser/mcs/namespace.cs

@ -674,11 +674,9 @@ namespace Mono.CSharp { @@ -674,11 +674,9 @@ namespace Mono.CSharp {
}
}
public IEnumerable<string> Conditionals {
public IDictionary<string, bool> Conditionals {
get {
if (conditionals == null)
return Enumerable.Empty<string> ();
return conditionals.Where (kv => kv.Value).Select (kv => kv.Key);
return conditionals ?? new Dictionary<string, bool> ();
}
}

22
ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/PreprocessorDirectiveTests.cs

@ -66,7 +66,6 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope @@ -66,7 +66,6 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope
Assert.AreEqual(new TextLocation(4, 8), pp.Last().EndLocation);
}
[Ignore("Fixme!")]
[Test]
public void NestedInactiveIf()
{
@ -94,7 +93,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope @@ -94,7 +93,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope
Roles.Comment,
Roles.PreProcessorDirective,
Roles.RBrace
}, ns.Children.Select(c => c.Role).ToArray());
}, ns.Children.Where (c => !(c is NewLineNode)).Select(c => c.Role).ToArray());
}
[Ignore("Fixme!")]
@ -118,19 +117,19 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope @@ -118,19 +117,19 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope
Roles.Comment,
Roles.PreProcessorDirective,
Roles.RBrace
}, ns.Children.Select(c => c.Role).ToArray());
}, ns.Children.Where (c => !(c is NewLineNode)).Select(c => c.Role).ToArray());
Assert.AreEqual(CommentType.SingleLine, ns.GetChildrenByRole(Roles.Comment).First().CommentType);
Assert.AreEqual(CommentType.InactiveCode, ns.GetChildrenByRole(Roles.Comment).Last().CommentType);
}
[Ignore("Fixme!")]
[Test]
public void PragmaWarning()
{
string program = "#pragma warning disable 809";
var ppd = ParseUtilCSharp.ParseGlobal<PreProcessorDirective>(program);
var ppd = ParseUtilCSharp.ParseGlobal<PragmaWarningPreprocssorDirective>(program);
Assert.AreEqual(PreProcessorDirectiveType.Pragma, ppd.Type);
Assert.AreEqual("warning disable 809", ppd.Argument);
Assert.IsTrue(ppd.Disable);
Assert.IsTrue(ppd.WarningList.Contains (809));
}
const string elifProgram = @"
@ -141,20 +140,18 @@ class B { } @@ -141,20 +140,18 @@ class B { }
#endif";
[Test]
[Ignore("parser bug (missing comment node)")]
public void ElifBothFalse()
{
CSharpParser parser = new CSharpParser();
var syntaxTree = parser.Parse(elifProgram, "elif.cs");
Assert.IsFalse(parser.HasErrors);
Assert.AreEqual(new Role[] {
Roles.PreProcessorDirective,
Roles.Comment,
Roles.PreProcessorDirective,
Roles.Comment,
Roles.PreProcessorDirective
}, syntaxTree.Children.Select(c => c.Role).ToArray());
}, syntaxTree.Children.Where (c => !(c is NewLineNode)).Select(c => c.Role).ToArray());
var aaa = syntaxTree.GetChildrenByRole(Roles.PreProcessorDirective).ElementAt(0);
Assert.IsFalse(aaa.Take);
Assert.AreEqual(PreProcessorDirectiveType.If, aaa.Type);
@ -167,7 +164,6 @@ class B { } @@ -167,7 +164,6 @@ class B { }
}
[Test]
[Ignore("parser bug (bbb.Take is true, should be false)")]
public void ElifBothTrue()
{
CSharpParser parser = new CSharpParser();
@ -181,7 +177,7 @@ class B { } @@ -181,7 +177,7 @@ class B { }
Roles.PreProcessorDirective,
Roles.Comment,
Roles.PreProcessorDirective
}, syntaxTree.Children.Select(c => c.Role).ToArray());
}, syntaxTree.Children.Where (c => !(c is NewLineNode)).Select(c => c.Role).ToArray());
var aaa = syntaxTree.GetChildrenByRole(Roles.PreProcessorDirective).ElementAt(0);
Assert.IsTrue(aaa.Take);
Assert.AreEqual(PreProcessorDirectiveType.If, aaa.Type);
@ -194,7 +190,6 @@ class B { } @@ -194,7 +190,6 @@ class B { }
}
[Test]
[Ignore("parser bug (bbb.Take is true, should be false)")]
public void ElifFirstTaken()
{
CSharpParser parser = new CSharpParser();
@ -208,7 +203,7 @@ class B { } @@ -208,7 +203,7 @@ class B { }
Roles.PreProcessorDirective,
Roles.Comment,
Roles.PreProcessorDirective
}, syntaxTree.Children.Select(c => c.Role).ToArray());
}, syntaxTree.Children.Where (c => !(c is NewLineNode)).Select(c => c.Role).ToArray());
var aaa = syntaxTree.GetChildrenByRole(Roles.PreProcessorDirective).ElementAt(0);
Assert.IsTrue(aaa.Take);
Assert.AreEqual(PreProcessorDirectiveType.If, aaa.Type);
@ -247,7 +242,6 @@ class B { } @@ -247,7 +242,6 @@ class B { }
}
[Test]
[Ignore("parser bug (BBB is missing)")]
public void ConditionalSymbolTest()
{
const string program = @"// Test

Loading…
Cancel
Save