Skip to content

Commit

Permalink
Issue with preprocessor directives.
Browse files Browse the repository at this point in the history
  • Loading branch information
matejmiklecic committed Feb 11, 2014
1 parent 4730eca commit 505c974
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
19 changes: 12 additions & 7 deletions ICSharpCode.NRefactory.CSharp/IndentEngine/CSharpIndentEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,15 @@ public class CSharpIndentEngine : IStateMachineIndentEngine
internal HashSet<string> customConditionalSymbols;

/// <summary>
/// True if any of the preprocessor if/elif directives in the current
/// block (between #if and #endif) were evaluated to true.
/// Stores the results of evaluations of the preprocessor if/elif directives
/// in the current block (between #if and #endif).
/// </summary>
internal CloneableStack<bool> ifDirectiveEvalResult = new CloneableStack<bool> ();
internal CloneableStack<bool> ifDirectiveEvalResults = new CloneableStack<bool> ();

/// <summary>
/// Stores the indentation levels of the if directives in the current block.
/// </summary>
internal CloneableStack<Indent> ifDirectiveIndents = new CloneableStack<Indent>();

/// <summary>
/// Stores the last sequence of characters that can form a
Expand Down Expand Up @@ -244,7 +249,6 @@ public bool EnableCustomIndentLevels
/// </summary>
internal char previousNewline = '\0';


/// <summary>
/// Current indent level on this line.
/// </summary>
Expand Down Expand Up @@ -310,7 +314,6 @@ public CSharpIndentEngine(CSharpIndentEngine prototype)

this.wordToken = new StringBuilder(prototype.wordToken.ToString());
this.previousKeyword = string.Copy(prototype.previousKeyword);
this.ifDirectiveEvalResult = prototype.ifDirectiveEvalResult;

this.offset = prototype.offset;
this.line = prototype.line;
Expand All @@ -323,7 +326,8 @@ public CSharpIndentEngine(CSharpIndentEngine prototype)
this.currentIndent = new StringBuilder(prototype.CurrentIndent.ToString());
this.lineBeganInsideMultiLineComment = prototype.lineBeganInsideMultiLineComment;
this.lineBeganInsideVerbatimString = prototype.lineBeganInsideVerbatimString;
this.ifDirectiveEvalResult = prototype.ifDirectiveEvalResult.Clone();
this.ifDirectiveEvalResults = prototype.ifDirectiveEvalResults.Clone();
this.ifDirectiveIndents = prototype.ifDirectiveIndents.Clone();

this.EnableCustomIndentLevels = prototype.EnableCustomIndentLevels;
}
Expand Down Expand Up @@ -427,7 +431,8 @@ public void Reset()
{
currentState = new GlobalBodyState(this);
conditionalSymbols.Clear();
ifDirectiveEvalResult.Clear();
ifDirectiveEvalResults.Clear();
ifDirectiveIndents.Clear();

offset = 0;
line = 1;
Expand Down
41 changes: 31 additions & 10 deletions ICSharpCode.NRefactory.CSharp/IndentEngine/IndentState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,8 +1267,8 @@ public override void Push(char ch)
switch (DirectiveType)
{
case PreProcessorDirective.If:
Engine.ifDirectiveEvalResult.Push(eval(DirectiveStatement.ToString()));
if (Engine.ifDirectiveEvalResult.Peek())
Engine.ifDirectiveEvalResults.Push(eval(DirectiveStatement.ToString()));
if (Engine.ifDirectiveEvalResults.Peek())
{
// the if/elif directive is true -> continue with the previous state
}
Expand All @@ -1280,19 +1280,19 @@ public override void Push(char ch)
}
break;
case PreProcessorDirective.Elif:
if (Engine.ifDirectiveEvalResult.Count > 0)
if (Engine.ifDirectiveEvalResults.Count > 0)
{
if (!Engine.ifDirectiveEvalResult.Peek())
if (!Engine.ifDirectiveEvalResults.Peek())
{
Engine.ifDirectiveEvalResult.Pop();
Engine.ifDirectiveEvalResults.Pop();
goto case PreProcessorDirective.If;
}
}
// previous if was true -> comment
ChangeState<PreProcessorCommentState>();
break;
case PreProcessorDirective.Else:
if (Engine.ifDirectiveEvalResult.Count > 0 && Engine.ifDirectiveEvalResult.Peek())
if (Engine.ifDirectiveEvalResults.Count > 0 && Engine.ifDirectiveEvalResults.Peek())
{
// some if/elif directive was true -> change to a state that will
// ignore any chars until #endif
Expand All @@ -1319,7 +1319,8 @@ public override void Push(char ch)
break;
case PreProcessorDirective.Endif:
// marks the end of this block
Engine.ifDirectiveEvalResult.Pop();
Engine.ifDirectiveEvalResults.Pop();
Engine.ifDirectiveIndents.Pop();
break;
case PreProcessorDirective.Region:
case PreProcessorDirective.Pragma:
Expand All @@ -1337,7 +1338,14 @@ public override void InitializeState()
// OPTION: IndentPreprocessorStatements
if (Engine.formattingOptions.IndentPreprocessorDirectives)
{
ThisLineIndent = Parent.ThisLineIndent.Clone();
if (Engine.ifDirectiveIndents.Count > 0)
{
ThisLineIndent = Engine.ifDirectiveIndents.Peek().Clone();
}
else
{
ThisLineIndent = Parent.ThisLineIndent.Clone();
}
}
else
{
Expand Down Expand Up @@ -1389,6 +1397,10 @@ public override void CheckKeyword(string keyword)
{
ThisLineIndent = Parent.NextLineIndent.Clone();
}
else if (DirectiveType == PreProcessorDirective.If)
{
Engine.ifDirectiveIndents.Push(ThisLineIndent.Clone());
}
}
}

Expand Down Expand Up @@ -1664,8 +1676,17 @@ public override void Push(char ch)

public override void InitializeState()
{
ThisLineIndent = Parent.NextLineIndent.Clone();
NextLineIndent = ThisLineIndent.Clone();
if (Engine.formattingOptions.IndentPreprocessorDirectives &&
Engine.ifDirectiveIndents.Count > 0)
{
ThisLineIndent = Engine.ifDirectiveIndents.Peek().Clone();
NextLineIndent = ThisLineIndent.Clone();
}
else
{
ThisLineIndent = Parent.NextLineIndent.Clone();
NextLineIndent = ThisLineIndent.Clone();
}
}

public override IndentState Clone(CSharpIndentEngine engine)
Expand Down
11 changes: 2 additions & 9 deletions ICSharpCode.NRefactory.Tests/IndentationTests/BlockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,6 @@ public static void Main (string[] args)
Assert.AreEqual("\t\t\t", indent.NextLineIndent);
}

[Ignore("Fixme")]
[Test]
public void TestPreprocessorIndenting_Case1()
{
Expand All @@ -1096,7 +1095,7 @@ static void Foo (int arg)
{
#if !DEBUG
if (arg > 0) {
$#else
#else$
if (arg < 0) {
#endif
Expand All @@ -1108,10 +1107,8 @@ public static void Main ()
}
}", policy);
Assert.AreEqual("\t\t", indent.ThisLineIndent);
Assert.AreEqual("\t\t", indent.NextLineIndent);
}

[Ignore("Fixme")]
[Test]
public void TestPreprocessorIndenting_Case2()
{
Expand All @@ -1127,7 +1124,7 @@ static void Foo (int arg)
#if !DEBUG
if (arg > 0) {
#else
$if (arg < 0) {
if (arg < 0) {$
#endif
}
Expand Down Expand Up @@ -1169,9 +1166,5 @@ public static void Main ()
Assert.AreEqual("\t\t\t", indent.ThisLineIndent);
Assert.AreEqual("\t\t\t", indent.NextLineIndent);
}




}
}

0 comments on commit 505c974

Please sign in to comment.