private void AppendIncludeFile(PreprocessorParams p, string fileName, bool searchSameDir) { // Load the include file string[] parentFiles; if (string.IsNullOrEmpty(p.fileName)) { parentFiles = p.parentFiles; if (parentFiles == null) { parentFiles = new string[0]; } } else { if (p.parentFiles != null) { parentFiles = p.parentFiles.Concat(new string[] { p.fileName }).ToArray(); } else { parentFiles = new string[0]; } } var includeNode = _store.GetIncludeFile(_appSettings, p.fileName, fileName, searchSameDir, parentFiles); if (includeNode == null) { return; } if (p.stopAtIncludeFile != null && includeNode.FullPathName.Equals(p.stopAtIncludeFile, StringComparison.OrdinalIgnoreCase)) { p.result.IncludeFileReached = true; return; } var rawSource = includeNode.GetSource(_appSettings); if (rawSource == null) { return; } var reader = new CodeSource.CodeSourcePreprocessorReader(rawSource); // Run the preprocessor on the include file. var includeSource = new CodeSource(); var parms = new PreprocessorParams(reader, includeSource, includeNode.FullPathName, parentFiles, p.fileContext, p.contentType, p.stopAtIncludeFile); p.result.Merge(Preprocess(parms)); p.writer.Append(includeSource); foreach (var preMergeFileName in includeNode.PreMergeFileNames) { AddIncludeDependency(preMergeFileName, true, false, includeNode.GetPreMergeContent(preMergeFileName)); } }
public Definition[] GetIncludeParentDefinitions(ProbeAppSettings appSettings, string includePathName) { Definition[] cachedDefs; if (_includeParentDefs.TryGetValue(includePathName.ToLower(), out cachedDefs)) { return(cachedDefs); } Log.Debug("Getting include file parent definitions: {0}", includePathName); IEnumerable <string> parentFileNames; using (var searcher = ProbeToolsPackage.Instance.FunctionFileScanner.CurrentApp.CreateSearcher()) { parentFileNames = searcher.GetIncludeParentFiles(includePathName, 3); // TODO: make limit configurable } Definition[] commonDefines = null; if (!parentFileNames.Any()) { Log.Debug("This file is not included by any other file."); commonDefines = new Definition[0]; _includeParentDefs[includePathName.ToLower()] = commonDefines; return(commonDefines); } foreach (var parentPathName in parentFileNames) { Log.Debug("Preprocessing include parent: {0}", parentPathName); var merger = new FileMerger(); merger.MergeFile(appSettings, parentPathName, null, false, true); var source = merger.MergedContent; var reader = new CodeSource.CodeSourcePreprocessorReader(source); var prepSource = new CodeSource(); var fileContext = FileContextUtil.GetFileContextFromFileName(parentPathName); var prep = new Preprocessor(appSettings, this); var prepResult = prep.Preprocess(reader, prepSource, parentPathName, new string[0], fileContext, includePathName); if (!prepResult.IncludeFileReached) { Log.Warning("Include file not reached when preprocessing parent.\r\nInclude File: {0}\r\nParent File: {1}", includePathName, parentPathName); continue; } var defs = prep.ActiveDefineDefinitions; if (!defs.Any()) { // No defines will be common Log.Debug("No definitions found in include parent file: {0}", parentPathName); commonDefines = new Definition[0]; break; } if (commonDefines == null) { commonDefines = defs.ToArray(); Log.Debug("{1} definition(s) found in include parent file: {0}", parentPathName, commonDefines.Length); } else { // Create array of defines common to all commonDefines = (from c in commonDefines where defs.Any(d => d.Name == c.Name) select c).ToArray(); Log.Debug("{1} definition(s) found in include parent file: {0}", parentPathName, commonDefines.Length); if (commonDefines.Length == 0) { break; } } } if (commonDefines == null) { commonDefines = new Definition[0]; } Log.Debug("Using {0} definition(s) from include parent files.", commonDefines.Length); _includeParentDefs[includePathName.ToLower()] = commonDefines; return(commonDefines); }
public CodeModel CreatePreprocessedModel(ProbeAppSettings appSettings, CodeSource source, string fileName, bool visible, string reason, IEnumerable <Preprocessor.IncludeDependency> includeDependencies) { #if DEBUG Log.Debug("Creating preprocessed model. Reason: {0}", reason); var startTime = DateTime.Now; #endif var reader = new CodeSource.CodeSourcePreprocessorReader(source); var prepSource = new CodeSource(); var defProvider = new DefinitionProvider(appSettings, fileName); var fileContext = FileContextUtil.GetFileContextFromFileName(fileName); var prep = new Preprocessor(appSettings, this); if (includeDependencies != null) { prep.AddIncludeDependencies(includeDependencies); } prep.Preprocess(reader, prepSource, fileName, new string[0], fileContext, stdlibDefines: _stdLibDefines); prep.AddDefinitionsToProvider(defProvider); if (fileContext == FileContext.Include && !string.IsNullOrEmpty(fileName)) { var includeParentDefs = GetIncludeParentDefinitions(appSettings, fileName); defProvider.AddGlobalFromFile(includeParentDefs); } #if DEBUG var midTime1 = DateTime.Now; #endif var prepModel = new PreprocessorModel(prepSource, defProvider, fileName, visible, prep.IncludeDependencies) { Preprocessor = prep }; #if DEBUG var midTime2 = DateTime.Now; #endif CodeModel modelToReturn; if (visible) { modelToReturn = CodeModel.CreateVisibleModelForPreprocessed(source, appSettings, this, prepModel); modelToReturn.PreprocessorModel = prepModel; modelToReturn.DisabledSections = prepSource.GenerateDisabledSections().ToArray(); } else { modelToReturn = CodeModel.CreateFullModelForPreprocessed(prepSource, appSettings, this, prepModel); modelToReturn.PreprocessorModel = prepModel; } modelToReturn.PreprocessorReferences = prep.References; #if DEBUG var endTime = DateTime.Now; var elapsedTime = endTime.Subtract(startTime).TotalMilliseconds; var prepTime = midTime1.Subtract(startTime).TotalMilliseconds; var modelTime = midTime2.Subtract(midTime1).TotalMilliseconds; var visTime = endTime.Subtract(midTime2).TotalMilliseconds; Log.Debug("Created model in {0} msecs ({1} preprocessor, {2} model, {3} visible)", elapsedTime, prepTime, modelTime, visTime); #endif return(modelToReturn); }