/*----------------------------------------------------------------------
Prof-It for C#
Copyright (c) 2004 Klaus Lehner, University of Linz
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
----------------------------------------------------------------------*/
using System.Collections;
using System.Text;
using System.IO;
using at.jku.ssw.ProfIt.Components;
using at.jku.ssw.ProfIt.Runtime;
namespace at.jku.ssw.ProfIt.CodeGen{
/// <summary>
/// This class generates instrumented code for a SourceFile
/// </summary>
public class CodeGenerator {
private static int currentId = 0;
public static void ResetCurrentId() {
currentId = 0;
}
public static int CurrentId { get { return currentId; } }
public static string Generate(SourceFile file) {
Solution solution = file.Project.Solution;
System.Text.StringBuilder builder = new StringBuilder();
string code = FileUtility.LoadTextFile(file.FileName);
if (file.Profile) {
foreach (BracePosition bracePos in file.Counters.Braces) {
code = code.Insert(bracePos.Position+1, bracePos.Value);
}
int index=0;
// should never happen
if (file.Counters.ClassCounters.Count == 0) return "";
int currentClassCounter = 0;
ClassCounter classCounter = (ClassCounter)file.Counters.ClassCounters[currentClassCounter++];
foreach (IBlock block in file.Counters.blocks) {
if (block.End < block.Start) {
block.End = block.Start + 1;
}
while (classCounter != null && classCounter.Start < block.Start) {
builder.Append(code.Substring(index, classCounter.BodyStart.Abs - index));
builder.Append("private static int[] ___profIt_Array = at.jku.ssw.ProfIt.Runtime.Incrementor.Get(System.Environment.CurrentDirectory + \"\\\\" + solution.Name + "_runtime.counters\").cnt;\r\n");
index = classCounter.BodyStart.Abs;
if (file.Counters.ClassCounters.Count == currentClassCounter) {
classCounter = null;
} else {
classCounter = (ClassCounter)file.Counters.ClassCounters[currentClassCounter++];
}
}
if (block is StatementBlock) {
builder.Append(code.Substring(index, block.Start.Abs - index));
builder.Append("___profIt_Array[" + currentId++ + "]++;");
index = block.Start.Abs;
}
}
solution.CounterArray.Counters.Add(file.Counters);
builder.Append(code.Substring(index));
code = builder.ToString();
}
if (SystemInfo.CreateProfiledSourceFiles) {
StreamWriter writer = new StreamWriter(file.FileName + "2");
writer.Write(code);
writer.Close();
}
return code;
}
}
}
|